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;
}
};
Longest Valid Parentheses
最新推荐文章于 2022-09-01 00:56:47 发布