leetcode 32 Longest Valid Parentheses

2018-06-04 12:29:06 浏览数 (1)

代码语言:javascript复制
class Solution {
public:
    int longestValidParentheses(string s) {
        int res = 0, start = 0;
        stack<int> m;
        for (int i = 0; i < s.size();   i) {
            if (s[i] == '(') m.push(i);
            else if (s[i] == ')') {
                if (m.empty()) start = i   1;
                else {
                    m.pop();
                    res = m.empty() ? max(res, i - start   1) : max(res, i - m.top());
                }
            }
        }
        return res;
    }
};

0 人点赞