1614.括号的最大嵌套深度
模拟
根据题意,其实就是求最大的连续左括号的数量(跳过普通字符,且与 )
抵消后),只需要边遍历边统计即可。
class Solution:
def maxDepth(self, s: str) -> int:
res = 0
cnt = 0
for c in s:
if c == '(':
cnt += 1
elif c == ')':
cnt -= 1
res = max(res, cnt)
return res
最后更新于