15Line Java Solution. Accepted


  • -1
    M

    public class Solution {
    ArrayList<String> result = new ArrayList<String>();
    public List<String> generateParenthesis(int n) {
    int left=n;
    int rigth=n;
    helper("",left,rigth);
    return result;
    }
    public void helper(String s, int L, int R){
    if(L>R)return;
    if(L==0&&R==0)result.add(s);
    if(L>0)helper(s+"(",L-1,R);
    if(R>0)helper(s+")",L,R-1);
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.