Generate Parentheses (Java)

public List<String> generateParenthesis(int n) {
    ArrayList<String> result = new ArrayList<String>();
    dfs(result, "", n, n);
    return result;
}
/*
left and right represents the remaining number of ( and ) that need to be added. 
When left > right, there are more ")" placed than "(". Such cases are wrong and the method stops. 
*/
public void dfs(ArrayList<String> result, String s, int left, int right){
    if(left > right)
        return;
 
    if(left==0&&right==0){
        result.add(s);
        return;
    }
 
    if(left>0){
        dfs(result, s+"(", left-1, right);
    }
 
    if(right>0){
        dfs(result, s+")", left, right-1);
    }
}

  

猜你喜欢

转载自www.cnblogs.com/wylwyl/p/10728872.html