题目描述:
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:
unordered_set<string> dup;
vector<string> generateParenthesis(int n) {
if (n == 1)
return vector<string>(1,"()");
vector<string> res;
vector<string> s = generateParenthesis(n - 1);
for (int i = 0; i < s.size(); i++){
for (int j = 0; j <= s[i].length(); j++){
string temp = s[i];
temp = temp.insert(j, "()");
if (dup.count(temp))
continue;
res.push_back(temp);
dup.insert(temp);
}
}
return res;
}
};
还有关于卡特兰数的方法
看这里,学习中……