leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0022.cpp (577B)


      1 class Solution {
      2     vector<string> res;
      3     stack<char> st;
      4     string crnt = "";
      5 
      6     void dfs(int n) {
      7         static const char o = '(', c = ')';
      8 
      9         if (!n) {
     10             if (st.empty()) res.push_back(crnt);
     11             return;
     12         }
     13 
     14         st.push(o), crnt += o;
     15         dfs(n - 1);
     16         st.pop(), crnt.pop_back();
     17 
     18         if (st.empty()) return;
     19 
     20         crnt += c, st.pop();
     21         dfs(n - 1);
     22         crnt.pop_back();
     23         st.push(c);
     24     }
     25 
     26   public:
     27     vector<string> generateParenthesis(int n) {
     28         dfs(n * 2);
     29         return res;
     30     }
     31 };