Concise JAVA solution


  • 0
    A
    public List<String> generateParenthesis(int n) {
    	List<String> list = new LinkedList<String>();
    	generateParenthesis(n, "", list, 0, 0);
    	return list;
    }
    
    public void generateParenthesis(int pairs, String str, List<String> list, int left, int right) {
    	if (str.length() == 2 * pairs) {
    		list.add(str);
    		return;
    	}
    	if (left < pairs) generateParenthesis(pairs, str + "(", list, left + 1, right);
    	if (right < left) generateParenthesis(pairs, str + ")", list, left, right + 1);
    }

Log in to reply
 

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