Wrong Answer: Output and Expected are the same. Test case: n = 1


  • 0
    P

    Input: 1
    Output: ["()"]
    Expected: ["()"]

    public class Solution {
        public ArrayList<String> generateParenthesis(int n) {
            ArrayList<String> arrayList = new ArrayList<String>();
            char[] A = new char[n*2 + 1];
            genParenthesis(n, n, A, 0, arrayList);
            return arrayList;
        }
    
        private void genParenthesis(int l, int r, char[] A, int index, ArrayList<String> arrayList) {
            if (l == 0 && r == 0) {
                String str = intArrtoStr(A);
                arrayList.add(str);
            } else {
                if (l > 0) {
                    A[index] = '(';
                    genParenthesis(l-1, r, A, index+1, arrayList);
                }
                if (r > l) {
                    A[index] = ')';
                    genParenthesis(l, r-1, A, index+1, arrayList);
                }
            }
        }
    
        private String intArrtoStr(char[] a) {
            StringBuilder sb = new StringBuilder();
            for (Character c: a) {
                sb.append(c);
            }
            return sb.toString();
        }
    }

Log in to reply
 

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