class Solution {
public:
int longestValidParentheses(string s) {
stack<int> stk;
vector<int> num(s.size(),0);
int mxx=0;
for(int i=0;i<s.size();++i){
if(s[i]=='(') stk.push(i);
else if(!stk.empty()){
int p=stk.top();
if(s[p]=='('){
num[i]=i-p+1;
if(p&&num[p-1]) num[i]+=num[p-1];
mxx=max(mxx,num[i]);
stk.pop();
}
}
}
return mxx;
}
};
leetcode 32. Longest Valid Parentheses
最新推荐文章于 2024-02-09 15:39:44 发布