遇到‘(’深度加1, ‘)’输出深度并减1, 查询list中最大深度即为当前深度 import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int depth = 0;
Scanner scanner = new Scanner(System.in);
String next = scanner.next();
List<Integer> list = new ArrayList<>();
int length = next.length();
for (int i = 0; i < length; i++) {
if ('(' == next.charAt(i)) {
depth += 1;
} else if (')' == next.charAt(i)) {
list.add(depth--);
}
}
if (list.size() == 0) {
System.out.println(0);
return;
}
for (Integer i : list) {
depth = (depth < i) ? i : depth;
}
System.out.println(depth);
}
}