标签:accept 组合 air 代码 vector function parent res +=
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> res; string s; traverse(res, s, n, n); return res; } void traverse(vector<string> &res,string &s, int n, int m) { if(0==n&&0==m) { res.push_back(s); return; } if(n>0) { s+=‘(‘; traverse(res,s,n-1,m); s.pop_back(); } if(m>n) { s+=‘)‘; traverse(res,s,n,m-1); s.pop_back(); } } };
标签:accept 组合 air 代码 vector function parent res +=
原文地址:https://www.cnblogs.com/lychnis/p/11756232.html