/******************************* 22. Generate Parentheses 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: [ "((()))", "(()())", "(())()", "()(())", "()()()" ] ********************************/ void generate(vector<string> &result,string s,int left,int right) { if (left == 0 && right == 0) result.push_back(s); if (left > 0) { generate(result, s + '(', left - 1, right); } if (right > 0 && left < right) { generate(result, s + ')', left , right-1); } } vector<string> generateParenteses(int n ) { vector<string> result; string s; generate(result,s,n,n); return result; }
【LeetCode 22】Generate Parentheses
最新推荐文章于 2021-01-30 12:42:36 发布