LeetCode-Generate Parentheses
2013-07-29 21:07
399 查看
class Solution { public: vector<string> generateParenthesis(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<string> vec; getParenthesis("", n, n, vec); return vec; } void getParenthesis(string str, int cntLeft, int cntRight, vector<string> &vec) { if (cntLeft == 0) { if (cntRight > 0) { getParenthesis(str + ")", cntLeft, cntRight - 1, vec); } else if (cntRight == 0 && str != "") { vec.push_back(str); } } else if (cntLeft == cntRight) { getParenthesis(str + "(", cntLeft - 1, cntRight, vec); } else { getParenthesis(str + "(", cntLeft - 1, cntRight, vec); getParenthesis(str + ")", cntLeft, cntRight - 1, vec); } } };
相关文章推荐
- LeetCode-Generate Parentheses & Letter Combinations of a Phone Number
- LeetCode: Generate Parentheses
- LeetCode 笔记系列五 Generate Parentheses
- leetcode: Generate Parentheses
- Leetcode: Generate Parentheses
- leetcode Generate Parentheses
- LeetCode——Generate Parentheses
- LeetCode Generate Parentheses
- Generate Parentheses - Leetcode
- [leetcode 22]generate parentheses
- [LeetCode] Generate Parentheses
- leetcode之 Generate Parentheses
- leetcode-Generate Parentheses
- [LeetCode]Generate Parentheses
- [LeetCode 22] Generate Parentheses
- LeetCode - Generate Parentheses
- 【LeetCode】C# 22、Generate Parentheses
- leetcode 022 —— Generate Parentheses
- [LeetCode] Generate Parentheses
- [LeetCode] Generate Parentheses