瞎写的,居然过了
#include <ios>
#include <vector>
class Solution {
public:
/**
*
* @param n int整型
* @return string字符串vector
*/
vector<string> ans;
string path;
vector<string> generateParenthesis(int n) {
// write code here
recursion(n, 0, 0);
return ans;
}
void recursion(int n, int left, int right){
if (n*2 == path.size()) {
ans.push_back(path);
return;
}
if (left < n) {
path += '(';
recursion(n, left+1, right);
path.pop_back();
}
if (right < left) {
path += ')';
recursion(n, left, right + 1);
path.pop_back();
}
}
};