My easy Java solution


  • 0
    X
    List<String> list = new ArrayList<String>();
    
    public List<String> generateParenthesis(int n) {
    	generate(n, n, "");
    	return list;
    }
    
    private void generate(int left, int right, String s) {
    	if (left == 0 && right == 0)
    		list.add(s);
    	if (left > 0)
    		generate(left - 1, right, s + "(");
    	if (right > 0 && right > left)
    		generate(left, right - 1, s + ")");
    }

Log in to reply
 

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