1614. Maximum Nesting Depth of the Parentheses
约 1 分钟
LeetCode 每日一题:Maximum Nesting Depth of the Parentheses
Given a valid parentheses string s, return the nesting depth of **s. The nesting depth is the maximum number of nested parentheses.
Constraints:
1 <= s.length <= 100sconsists of digits0-9and characters'+','-','*','/','(', and')'.- It is guaranteed that parentheses expression
sis a VPS.
思路
太他妈的简单了 代码直接贴上
算了还是讲一下思路,就好像让你不用max()求数组内最大的数字一样,你挨个遍历,记录最大的那个。同理,你挨个遍历括号,记录括号的最大深度就可以了
python
class Solution:
def maxDepth(self, s: str) -> int:
cur = 0
max_num = 0
for c in s:
if c == '(':
cur += 1
max_num = max(max_num, cur)
elif c == ')':
cur -= 1
return max_num时空复杂度都是O(n)
系列每日一题 →