这道题的关键是只要l>=r,就可以进行下去,相当于消除了合适的配对
class Solution {
private:
vector<string>v;
public:
vector<string> generateParenthesis(int n) {
get("",0,0,n);
return v;
}
void get(string s,int l,int r,int n)
{
if(r>l || l>n || r>n) return;
if(l == r && r == n) v.push_back(s);
get(s+'(',l+1,r,n);
get(s+')',l,r+1,n);
}
};