Description
牛客网 2018校招真题 括号匹配深度
Solving Ideas
与 缺失的括号 类似
Time complexity :
O
(
n
)
O(n)
O(n)
Space complexity :
O
(
1
)
O(1)
O(1)
Solution
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author wylu
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] str = br.readLine().toCharArray();
int res = 0, depth = 0;
for (char ch : str) {
if (ch == '(') {
depth++;
res = Math.max(res, depth);
} else {
depth--;
}
}
System.out.println(res);
}
}