32. 最长有效括号
题目链接:32. 最长有效括号
代码如下:
//栈
//参考:https://leetcode.cn/problems/longest-valid-parentheses/solutions/314683/zui-chang-you-xiao-gua-hao-by-leetcode-solution
class Solution
{
public:
int longestValidParentheses(string s)
{
int res=0;
stack<int> sta;
sta.push(-1);
for(int i=0;i<s.size();i++)
{
if(s[i]=='(')
{
sta.push(i);
}
else
{
sta.pop();
if(sta.empty())
{
sta.push(i);
}
else
{
res=max(res,i-sta.top());
}
}
}
return res;
}
};
标签:sta,int,32,括号,res,push,最长
From: https://blog.csdn.net/weixin_45256307/article/details/139874036