题目:
题解:
int longestValidParentheses(char* s) {
int n = strlen(s);
int left = 0, right = 0, maxlength = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '(') {
left++;
} else {
right++;
}
if (left == right) {
maxlength = fmax(maxlength, 2 * right);
} else if (right > left) {
left = right = 0;
}
}
left = right = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '(') {
left++;
} else {
right++;
}
if (left == right) {
maxlength = fmax(maxlength, 2 * left);
} else if (left > right) {
left = right = 0;
}
}
return maxlength;
}