![leetcode-algorithms-22 Generate Parentheses leetcode-algorithms-22 Generate Parentheses](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
leetcode-algorithms-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:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
解法
class Solution {
public:
vector<string> generateParenthesis(int n)
{
std::vector<std::string> result;
addParenth(result, "", n, 0);
return result;
}
void addParenth(std::vector<std::string> &v, std::string str, int n, int m)
{
if(n==0 && m==0)
{
v.push_back(str);
return;
}
if(m > 0)
addParenth(v, str+")", n, m-1);
if(n > 0)
addParenth(v, str+"(", n-1, m+1);
}
};