统计法
借助变量cur
来统计括号的出现次数做互相抵消。res
则记录抵消次数,最终结果返回res
class Solution:
def maxDepth(self, s: str) -> int:
res = 0
cur = 0
for char in s:
if char == "(": res += 1
if char == ")":
res -= 1
cur = max(res, cur)
return cur
复杂度分析
- 时间复杂度: O(N)
- 空间复杂度: O(1)