1614. Maximum Nesting Depth of the Parentheses

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int maxDepth(String s) {
int maxDepth = 0;

int leftBrackets = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(') {
leftBrackets++;
maxDepth = Math.max(maxDepth, leftBrackets); // 题目保证了表达式 s 是有效的括号表达式
} else if (c == ')') {
leftBrackets--;
}
}

return maxDepth;
}
}

References

1614. Maximum Nesting Depth of the Parentheses