标签:
Nothing fancy, just use recursive.
1 class Solution { 2 public: 3 void getP(vector<string> &result, string current, int left, int right) { 4 if (left == 0 && right == 0) { 5 result.push_back(current); 6 return; 7 } 8 if (left > 0) { 9 getP(result, current + ‘(‘, left - 1, right + 1); 10 } 11 if (right > 0) { 12 getP(result, current + ‘)‘, left, right - 1); 13 } 14 } 15 vector<string> generateParenthesis(int n) { 16 vector<string> result; 17 getP(result, "", n, 0); 18 return result; 19 } 20 };
LeetCode – Refresh – Generate Parentheses
标签:
原文地址:http://www.cnblogs.com/shuashuashua/p/4352212.html