以下程序实现了这一功能,请你填补空白处内容:
```java
import java.util.*;
class Solution {
public int longestValidParentheses(String s) {
int left = 0, right = 0, max = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(')
left++;
else
right++;
if (left == right)
max = Math.max(max, left * 2);
if (right > left)
left = right = 0;
}
left = 0;
right = 0;
for (int i = s.length() - 1; i >= 0; i--) {
__________________;
if (left == right)
max = Math.max(max, left * 2);
if (right < left)
left = right = 0;
}
return max;
}
}
```
## template
```java
import java.util.*;
class Solution {
public int longestValidParentheses(String s) {
int left = 0, right = 0, max = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(')
left++;
else
right++;
if (left == right)
max = Math.max(max, left * 2);
if (right > left)
left = right = 0;
}
left = 0;
right = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == '(')
left++;
else
right++;
if (left == right)
max = Math.max(max, left * 2);
if (right < left)
left = right = 0;
}
return max;
}
}
```
## 答案
```java
if (s.charAt(i) == '(')
left++;
else
right++;
```
## 选项
### A
```java
if (s.charAt(i) == ')')
left++;
else
right++;
```
### B
```java
if (s.charAt(i) == '(')
right++;
else
left++;
```
### C
```java
if (s.charAt(i) == ')')
right++;
else
left++;
```