LeetCode: Generate Parentheses
2012-11-29 14:45
453 查看
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
class Solution { public: vector<string> generateParenthesis(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int lp = n; int rp = n; vector<string> res; string str = ""; generate(res, str, lp, rp); return res; } void generate(vector<string> &res, string str, int lp, int rp){ if(lp == 0 && rp == 0){ res.push_back(str); return; } string temp = str; if(lp > 0){ temp+="("; generate(res, temp, lp -1, rp); } temp = str; if(rp > lp){ temp += ")"; generate(res, temp, lp, rp - 1); } return; } };
相关文章推荐
- leetcode-22-Generate Parentheses
- [leetcode] Generate Parentheses
- LeetCode Generate Parentheses DFS或者生产呢排列
- [LeetCode]Generate Parentheses
- Generate Parentheses -- leetcode
- LeetCode_Generate Parentheses
- [leetcode]Generate Parentheses
- LeetCode-22-Generate Parentheses(DFS/递归)-Medium
- 【Leetcode】Generate Parentheses
- 【leetcode】Generate Parentheses
- [LeetCode]Generate Parentheses
- LeetCode 22 Generate Parentheses(生成括号)
- LeetCode22——Generate Parentheses(给定n对括号,然后看有多少正确的括号组合)
- 【leetcode】Generate Parentheses
- 【LeetCode】Generate Parentheses 解题报告
- Leetcode-Generate Parentheses
- [Leetcode] 22- Generate Parentheses
- 【leetcode】Generate Parentheses
- [LeetCode]题解(python):022-Generate Parentheses
- Generate Parentheses - LeetCode 22