递归的使用
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @return string字符串vector
*/
vector<string> generateParenthesis(int n) {
if (n == 0) {
return std::vector<std::string>();
}
std::string tmp;
std::vector<std::string> str;
recursion(0, 0, n, str, tmp);
return str;
}
private:
void recursion(int left, int right, const int &n, std::vector<std::string> &str, std::string tmp) {
if (left == n && right == n) {
str.push_back(tmp);
return ;
}
if (left < n) {
recursion(left + 1, right, n, str, tmp + '(');
}
if (right < left && right < n) {
recursion(left, right + 1, n, str, tmp + ')');
}
}
};