[Time Limit Exceeded] I'm not sure if the code is logically right and only has time complexity problem?


  • 0
    H
    public class Solution {
    public ArrayList<String> generateParenthesis(int n) {
         ArrayList<String> pairs= new ArrayList<String>();
        if (n == 0)
            return pairs;
        if (n == 1){
            pairs.add("()");
            return pairs;
        }
        ArrayList<String> cpairs = generateParenthesis(n-1);
        for (String s : cpairs){
            char[] pare = new char[]{'(', ')'};
            for (int i = 0; i<= s.length(); i++){
                StringBuilder sb = new StringBuilder(s);
                sb.insert(i, pare);
                pairs.add(sb.toString());
            }
        }
        return pairs;
    }
    

    }


  • 0
    Y
    This post is deleted!

  • 0
    Y
    This post is deleted!

Log in to reply
 

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