Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created November 4, 2018 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/12b8781e005dd018aa67e16af3de17ba to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/12b8781e005dd018aa67e16af3de17ba to your computer and use it in GitHub Desktop.
vector<string>res;
class Solution {
public:
void choose(string prix,int left,int right,int times,int num){
if(times==num){
string temp =prix+")";
res.push_back(temp);
return;
}
if(left<num/2){
string temp =prix+"(";
choose(temp,left+1,right,times+1,num);
}
if(right<left){
string temp =prix+")";
choose(temp,left,right+1,times+1,num);
}
}
vector<string> generateParenthesis(int n) {
int num =n*2;
int left =0,right=0;
res.clear();
if(n==0) return res;
int times =0;
string prix ="";
choose(prix,left,right,times+1,num);
return res;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment