2ms AC JAVA Solution using recursive call


  • 20
    H
    public class Solution {
        public List<String> generateParenthesis(int n) {
            ArrayList<String> m=new ArrayList<>();
            generate(m, "", n, n);
            return m;
        }
        public void generate(ArrayList m, String s, int l, int r){
            if(l==0 && r==0){ 
                m.add(s);
                return;
            }
            if(l>0) generate(m, s+"(",  l-1,  r);
            if(r>l) generate(m, s+")",  l,  r-1);
        }
        
        
    }

  • 0
    Z
    This post is deleted!

  • 0
    Y

    can you explain why there is no duplicated in the result?


Log in to reply
 

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