标签:
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
Hide Tags
class Solution { public: vector<string> generateParenthesis(int n) { vector<string> res; generateParenthesis(res,"",n,n); return res; } void generateParenthesis(vector<string>& res,string str,int left,int right){ if(left ==0 && right == 0 ){ res.push_back(str); return ; } if(left>0) generateParenthesis(res,str+"(",left-1,right); if(right>left) generateParenthesis(res,str+")",left,right-1); } };
标签:
原文地址:http://www.cnblogs.com/zengzy/p/5059845.html