leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0022.cpp (577B)
0 class Solution {
1 vector<string> res;
2 stack<char> st;
3 string crnt = "";
5 void dfs(int n) {
6 static const char o = '(', c = ')';
8 if (!n) {
9 if (st.empty()) res.push_back(crnt);
10 return;
11 }
13 st.push(o), crnt += o;
14 dfs(n - 1);
15 st.pop(), crnt.pop_back();
17 if (st.empty()) return;
19 crnt += c, st.pop();
20 dfs(n - 1);
21 crnt.pop_back();
22 st.push(c);
23 }
25 public:
26 vector<string> generateParenthesis(int n) {
27 dfs(n * 2);
28 return res;
29 }
30 };