#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
vector<string> generateParenthesis(int n) {
int left = n;
int right = n;
vector<string> res ;
string str = "";
helper(res,left,right,str);
return res;
}
public:
void helper(vector<string> &res,int left,int right,string str){
if(left > right){
return ;
}
if(left == 0 && right == 0){
res.push_back(str);
return;
}
if(left > 0){
helper(res,left-1,right,str+"(");
}
if(right > 0){
helper(res,left,right-1,str+")");
}
}
};
int main(){
Solution so;
int n = 3;
vector<string> res = so.generateParenthesis(n);
int size = res.size();
for(int i = 0; i< size ; i++ ){
cout << "item is: " << res[i] << endl;
}
}
07-20
07-20
07-20
07-20
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交