Longest Valid Parentheses

来源:互联网 发布:h3c端口删除vlan命令 编辑:程序博客网 时间:2024/05/29 13:56
class Solution {public:    int longestValidParentheses(string s)     {        const int size = s.size();        int start = -1;        int res = 0;        stack<int> stackdata;        for(int i=0 ; i<size;++i)        {            if (s[i] == '(')                stackdata.push(i);            else            {                if(!stackdata.empty())                {                    stackdata.pop();                    if(!stackdata.empty())                        res = max(res, i - stackdata.top());                    else                        res = max(res, i - start);                }                else                    start = i;            }        }        return res;            }};

0 0