[leetcode-22-Generate Parentheses]
Posted hellowOOOrld
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode-22-Generate Parentheses]相关的知识,希望对你有一定的参考价值。
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:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
思路:
DFS.
void addParenthesis(vector<string>& res, string str, int open, int close,int n) { if (str.length() == n*2) { res.push_back(str); return; } if (open<n)addParenthesis(res, str + "(", open + 1, close, n); if (close < open)addParenthesis(res, str + ")", open, close + 1, n); } vector<string> generateParenthesis(int n) { vector<string>res; addParenthesis(res, "", 0, 0, n); return res; }
参考:
https://discuss.leetcode.com/topic/4485/concise-recursive-c-solution
以上是关于[leetcode-22-Generate Parentheses]的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode 22. Generate Parentheses
leetcode22. Generate Parentheses
leetcode 22 -- Generate Parentheses
[leetcode-22-Generate Parentheses]