力扣32.最长有效括号(栈)
32. 最长有效括号 - 力扣(LeetCode)
代码区:
#include<stack>
#include<string>
/*最长有效*/
class Solution {
public:
int longestValidParentheses(string s) {
stack<int> st;
int ans=0;
int n=s.length();
st.push(-1);
for(int i=0;i<n;i++){
if(s[i]=='('){
st.push(i);
}
else {
st.pop();
if(st.empty()){
st.push(i);
}
else{
ans=max(ans,i-st.top());
}
}
}
return ans;
}
};
欢迎各位读者提出意见。
(菜菜奋斗小日记)