链接:https://leetcode-cn.com/problems/generate-parentheses/
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。
例如,给出 n = 3,生成结果为:
[ "((()))", "(()())", "(())()", "()(())", "()()()" ]
class Solution { public: vector<string>q; string p; int len; void dfs(int i,int num){ if(i==len)q.push_back(p); if(len-i-2>=num){ p+='('; dfs(i+1,num+1); p.pop_back(); } if(num>0){ p+=')'; dfs(i+1,num-1); p.pop_back(); } } vector<string> generateParenthesis(int n) { p=""; len=n*2; dfs(0,0); return q; } };