Java - Easy to undrestand


  • 0
    J
    public class Solution {
        public List<String> generateParenthesis(int n) {
            ArrayList<String> master = new ArrayList<String>();
            recur(n, n, "", master);
            return master;
            
        }
        
        public void recur(int open, int close, String current, ArrayList<String> master) {
            if(open == 0 && close == 0) master.add(current);
            
            if(open > 0) recur(open-1, close, current+"(", master);
            
            if(close > 0 && close > open) recur(open, close-1, current+")", master);
        }
    }
    

Log in to reply
 

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