简单的dfs。
class Solution {
public:
vector<string> generateParenthesis(int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ans.clear();
if(n == 0) return ans;
dfs(0, 0, "", n);
return ans;
}
void dfs(int lnum, int rnum, string str, int n){
if(str.size() == n*2){
ans.push_back(str);
return ;
}
if(lnum < n)
dfs(lnum+1, rnum, str+'(', n);
if(lnum > rnum)
dfs(lnum, rnum+1, str+')', n);
}
vector<string> ans;
};