22. Generate Parentheses
2018-01-23 10:01
295 查看
22. Generate Parentheses
DescriptionHintsSubmissionsDiscussSolutionDiscussPick
One
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:
[ "((()))", "(()())", "(())()", "()(())", "()()()" ]
卡特兰数:
class Solution { private: void generate(int leftNum,int rightNum,string s,vector<string> &result) { if(leftNum==0&&rightNum==0) { result.push_back(s); } if(leftNum>0) { generate(leftNum-1,rightNum,s+'(',result); } if(rightNum>0&&leftNum<rightNum) { generate(leftNum,rightNum-1,s+')',result); } } public: vector<string> generateParenthesis(int n) { vector<string>ans; generate(n, n, "", ans); return ans; } };
相关文章推荐
- leetcode 22:Generate Parentheses(15-10-9)
- 22. Generate Parentheses
- 22. Generate Parentheses
- 22. Generate Parentheses
- 22. Generate Parentheses
- LeetCode 22 Generate Parentheses
- LeetCode 22_Generate Parentheses
- LeetCode_22 Generate Parentheses
- [leetcode 22]Generate Parentheses-----n对括号可以组成的括号对序列
- 22. Generate Parentheses
- [Leetcode] 22- Generate Parentheses
- 22-m-Generate Parentheses
- 22. Generate Parentheses My Submissions QuestionEditorial Solution
- leetcode-22-Generate Parentheses
- 22. Generate Parentheses
- Leet Code 22 Generate Parentheses - 生成括号 - Java
- 回溯之Leetcode22 Generate Parentheses
- LeetCode 22 Generate Parentheses(生成括号)
- 22. Generate Parentheses
- LeetCode 22 Generate Parentheses (DFS 构造)