Leecode 22:
代码:
class Leecode22 {
std::vector<std::string> result;
std::vector<std::string> Leecode22_generateParenthesis(int n) {
dfs(n, n, "");
return result;
}
void dfs(int left, int right, std::string curStr) {
if (left == 0 && right == 0) { //左右括号全用完
result.push_back(curStr);
return;
}
if (left > 0)
dfs(left - 1, right, curStr + "("); //左括号还有
if (right > left)
dfs(left, right - 1, curStr + ")"); //右括号还有
}
};