Generate Parentheses

遍历:注意左括号要在右括号前!

 1 class Solution {
 2    vector<string> res;
 3 public:
 4     void set(int left,int right,vector<string>& result,string str)
 5     {
 6         if(!left &&!right)
 7         result.push_back(str);
 8         if(left>0)
 9         set(left-1,right,result,str+'(');
10         if(left<right &&right>0)
11         set(left,right-1,result,str+')');
12     }
13     vector<string> generateParenthesis(int n) {
14         string s;
15         set(n,n,res,s);
16         return res;
17     }
18 };

 

你可能感兴趣的:(Generate Parentheses)