class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return int整型
*/
int longestValidParentheses(string s) {
stack<int> stk;
int ans = 0;
for (int i = 0, start = -1; i < s.size(); i++) {
if (s[i] == '(') stk.push(i);
else {
if (!stk.empty()) {
stk.pop();
if (!stk.empty()) {
ans = max(ans, i - stk.top());
} else {
ans = max(ans, i - start);
}
} else {
start = i;
}
}
}
return ans;
}
};



京公网安备 11010502036488号