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:
"((()))", "(()())", "(())()", "()(())", "()()()"
暴力思路:其实就是组合,n=3,则有6个位置,每个位置可以插入'(',或者')',当n=3时,就有64中可能,只需对每一种可能作必要的筛选即可。
代码:
class Solution {private: char parenthesis[2]; vectorres; int num;public: void dfs(int dep,string temp){ if(dep==num){ stack s; for (int i=0;i generateParenthesis(int n) { parenthesis[0]='('; parenthesis[1]=')'; num=n*2; string temp=""; dfs(0,temp); return res; }};