
class Solution {
public:
int longestValidParentheses(string s) {
int ans=0;
stack<int>sk;
sk.push(-1);
for(int i=0;i<s.size();i++){
if(s[i]=='('){
sk.push(i);
}else {
sk.pop();
if(sk.empty()){
sk.push(i);
}else {
ans=max(ans,i-sk.top());
}
}
}
return ans;
}
};
原创
leetcode32. 最长有效括号
本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。