https://leetcode.com/problems/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:
[ "((()))", "(()())", "(())()", "()(())", "()()()" ]
代码:
class Solution { public: vector<string> generateParenthesis(int n) { vector<string> ans; dfs(n, n, "", ans); return ans; } void dfs(int L, int R, string out, vector<string> &ans) { if(L > R) return; if(L == 0 && R == 0) ans.push_back(out); else { if(L > 0) dfs(L - 1, R, out + '(', ans); if(R > 0) dfs(L, R - 1, out + ')', ans); } } };
被搜索支配的晚上