leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0032.cpp (504B)
0 class Solution { 1 public: 2 int longestValidParentheses(const string &s) const { 3 stack<int> st; 4 st.push(-1); 5 6 int res = 0; 7 for (int i = 0; i < s.size(); i++) { 8 if (s[i] == '(') 9 st.push(i); 10 else { 11 if (!st.empty()) st.pop(); 12 if (!st.empty()) 13 res = max(res, i - st.top()); 14 else 15 st.push(i); 16 } 17 } 18 19 return res; 20 } 21 };