@@ -0,0 +1,1347 @@
+package coding4fun;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A/**%0A * Generate all combinations of well-formed parentheses.%0A * %0A * %3Cpre%3E%0A * Input: 1%0A * Output: ()%0A * %0A * Input: 2%0A * Output: (()), ()()%0A * %0A * Input: 3%0A * Output: ((())), (()()), (())(), ()(()), ()()()%0A * %0A * Input: 4%0A * Output: (((()))), ((()())), ((())()), ((()))(), (()(())), (()()()), (()())(),%0A *         (())(()), (())()(), ()((())), ()(()()), ()(())(), ()()(()), ()()()()%0A * %3C/pre%3E%0A */%0Apublic class Problem19 %7B%0A    public static List%3CString%3E generateParentheses(int n) %7B%0A        List%3CString%3E parentheses = new ArrayList%3C%3E();%0A        generate(n, 0, 0, %22%22, parentheses);%0A        return parentheses;%0A    %7D%0A    %0A    private static void generate(int n, int nOpen, int nClose, String accu, List%3CString%3E result) %7B%0A        if (nClose == n) %7B%0A            result.add(accu);%0A            return;%0A        %7D%0A        if (nOpen %3C n) %7B%0A            generate(n, nOpen+1, nClose, accu + %22(%22, result);%0A        %7D%0A        if (nOpen %3E nClose) %7B%0A            generate(n, nOpen, nClose+1, accu + %22)%22, result);%0A        %7D%0A    %7D%0A    %0A    public static void main(String%5B%5D args) %7B%0A        System.out.println(generateParentheses(1));%0A        System.out.println(generateParentheses(2));%0A        System.out.println(generateParentheses(3));%0A        System.out.println(generateParentheses(4));%0A    %7D%0A%7D%0A

Add problem 19