Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. public List<String> generateParenthesis(int n) {
  2. List<String> list = new ArrayList<String>();
  3. backtrack(list, "", 0, 0, n);
  4. return list;
  5. }
  6.  
  7. public void backtrack(List<String> list, String str, int open, int close, int max){
  8.  
  9. if(str.length() == max*2){
  10. list.add(str);
  11. return;
  12. }
  13.  
  14. if(open < max)
  15. backtrack(list, str+"(", open+1, close, max);
  16. if(close < open)
  17. backtrack(list, str+")", open, close+1, max);
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement