You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classSolution {
public:intlongestValidParentheses(string s) {
int max_len = 0, last = -1; // the position of last '('
stack<int> lefts;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '(') {
lefts.push(i);
} else {
if (lefts.empty()) {
last = i;
} else {
lefts.pop();
if (lefts.empty()) {
max_len = max(max_len, i - last);
} else {
max_len = max(max_len, i - lefts.top());
}
}
}
}
return max_len;
}
};
The text was updated successfully, but these errors were encountered:
思路:
用栈来实现,没啥难度
The text was updated successfully, but these errors were encountered: