具体题解见IPad笔记
class Solution {
public:
int longestValidParentheses(string s) {
int res = 0;
stack<int> stk; //这里不要写成char类型,因为进栈的是i是下标,不是s[i]‘(’
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()) res = max(res, i - stk.top());
else res = max(res, i - start);
}
else start = i;
}
}
return res;
}
};