22. Generate Parentheses
作者:互联网
My back tracking solution:
class Solution { List<String> res = new ArrayList<>(); public List<String> generateParenthesis(int n) { backTracking(n, 0 , 0, new StringBuilder(), res); return res; } private void backTracking(int n, int lCount, int rCount, StringBuilder s, List<String> res){ if(lCount==n && rCount==n){ res.add(s.toString()); s = new StringBuilder(); return; } if(lCount<n){ s.append("("); backTracking(n, lCount+1, rCount, s, res); s.deleteCharAt(s.length()-1); } if(rCount<lCount){ s.append(")"); backTracking(n, lCount, rCount+1, s, res); s.deleteCharAt(s.length()-1); } } }
标签:Parentheses,22,int,res,List,lCount,StringBuilder,new,Generate 来源: https://www.cnblogs.com/feiflytech/p/16164382.html