有没有大佬看看我这个思路,到底要怎么改正。case=80.95%, import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()) {
String s = sc.nextLine();
Stack stack = new Stack<>();
stack.push(-1);
int ret = 0;
for (int i = 0; i
char c = s.charAt(i);
if (c == '(') {
stack.push(i);
}
if (c == ')') {
int pre = stack.pop();
if (stack.isEmpty()) {
stack.push(i);
} else {
if (stack.peek() >= 0 &&
isValid(s.substring(stack.peek(), i + 1))) {
ret = Math.max(ret, i - stack.peek());
} else {
if (isValid(s.substring(pre, i + 1))) {
ret = Math.max(ret, i - pre + 1);
}
}
}
}
}
System.out.println(ret);
}
}
private static boolean isValid(String s) {
for (int i = 0; i
if (s.charAt(i) != '(' && s.charAt(i) != ')') {
return false;
}
}
return true;
}
}