1737. Maximum Nesting Depth of the Parentheses
2026/1/12大约 1 分钟约 332 字
1737. Maximum Nesting Depth of the Parentheses
难度: Easy
题目描述
Given a valid parentheses string s, return the nesting depth ofs. The nesting depth is the maximum number of nested parentheses.
Example 1:
Input: s = "(1+(2*3)+((8)/4))+1"
Output: 3
Explanation:
Digit 8 is inside of 3 nested parentheses in the string.
Example 2:
Input: s = "(1)+((2))+(((3)))"
Output: 3
Explanation:
Digit 3 is inside of 3 nested parentheses in the string.
Example 3:
Input: s = "()(())((()()))"
Output: 3
Constraints:
1 <= s.length <= 100sconsists of digits0-9and characters'+','-','*','/','(', and')'.- It is guaranteed that parentheses expression
sis a VPS.
解题思路
代码实现
解决方案
java
class Solution {
public int maxDepth(String s) {
int max = 0;
int cur = 0;
char[] charArray = s.toCharArray();
for (char c : charArray) {
switch (c) {
case '(':
cur++;
max = Math.max(max, cur);
break;
case ')':
cur--;
break;
}
}
return max;
}
}