leetcode

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

0032.cpp (504B)


      1 class Solution {
      2   public:
      3     int longestValidParentheses(const string &s) const {
      4         stack<int> st;
      5         st.push(-1);
      6 
      7         int res = 0;
      8         for (int i = 0; i < s.size(); i++) {
      9             if (s[i] == '(')
     10                 st.push(i);
     11             else {
     12                 if (!st.empty()) st.pop();
     13                 if (!st.empty())
     14                     res = max(res, i - st.top());
     15                 else
     16                     st.push(i);
     17             }
     18         }
     19 
     20         return res;
     21     }
     22 };