parentheses is all but no sort


  • 0
    K
    public List<String> generateParenthesis(int n) {
    		List<String> result = new ArrayList<String>();
    		if (n < 1)
    			return result;
    		String left = "(";
    		String right = ")";
    		String parenter = "()";
    		if (n == 1) {
    			result.add(parenter);
    			return result;
    		}
    		List<String> litle = generateParenthesis(n - 1);
    		int size = litle.size();
    		for (int j = 0; j < litle.size(); j++) {
    			result.add(left + litle.get(j) + right);
    			result.add(parenter + litle.get(j));
    			if (size != (j+1)) {
    				result.add(litle.get(j) + parenter);
    			}
    		}
    
    		return result;
    	}
    

Log in to reply
 

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