Generate all Parentheses II
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| void helper(string& s, int open, int fin, vector<string>& res) { if(open == 0 and fin == 0) res.push_back(s); else { if(open < fin) { s.push_back('('); helper(s,open + 1, fin, res); s.pop_back(); } if(open) { s.push_back(')'); helper(s,open - 1, fin - 1, res); s.pop_back(); } } } vector<string> Solution::generateParenthesis(int A) { vector<string> res; helper(string() = "", 0, A, res); return res; }
|