Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
class Solution { public: vector<string> generateParenthesis(int n) { vector<string> re; par(re,"",n,0 , 0); return re; } void par(vector<string> &re,string now , int n ,int left ,int right) { if(left == n && right == n) { re.push_back(now); return; } if(left > right) { if(left < n) { par(re,now + '(' , n ,left+1 ,right); par(re,now + ')' , n ,left ,right+1); } else if(left == n) { for(int i = right ;i < n ; i++) { now+=')'; } re.push_back(now); return; } } else if(left == right) { par(re,now + '(' , n ,left+1 ,right); } } };