@@ -0,0 +1,1078 @@
+/***%0A * Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.%0A *%0A * For example, given n = 3, a solution set is:%0A *%0A * %22((()))%22, %22(()())%22, %22(())()%22, %22()(())%22, %22()()()%22%0A */%0Apublic class Solution %7B%0A    // Solution 1: S: O(2%5En), T: O(n)%0A    public List%3CString%3E generateParenthesis(int n) %7B%0A        List%3CString%3E ret = new ArrayList%3CString%3E();%0A        StringBuffer s = new StringBuffer(%22%22);%0A        if (n %3E 0) generate(ret, s, 0, 0, n);%0A        return ret;%0A    %7D%0A    private void generate(List%3CString%3E ret, StringBuffer s, int l, int r, int n) %7B%0A        if (n == l) %7B%0A            StringBuffer ns = new StringBuffer(s);%0A            while (n - r %3E 0) %7B%0A                ns.insert(')');%0A                r++;%0A            %7D%0A            ret.add(ns);%0A            return;%0A        %7D%0A        s.append('(');%0A        generate(ret, s, l + 1, r, n);%0A        s.deleteCharAt(s.length() - 1);%0A        if (l %3E r) %7B%0A            s.append(')');%0A            generate(ret, s, l, r + 1, n);%0A            s.deleteCharAt(s.length() - 1);%0A        %7D%0A    %7D%0A    %0A%7D%0A

add GenerateParentheses.java