Skip to content

Latest commit

 

History

History
83 lines (67 loc) · 2.08 KB

leecode1614.md

File metadata and controls

83 lines (67 loc) · 2.08 KB

1614. 括号的最大嵌套深度

地址:https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/

题目描述

如果字符串满足以下条件之一,则可以称之为 有效括号字符串(valid parentheses string,可以简写为 VPS):

字符串是一个空字符串 "",或者是一个不为 "(" 或 ")" 的单字符。 字符串可以写为 AB(A 与 B 字符串连接),其中 A 和 B 都是 有效括号字符串 。 字符串可以写为 (A),其中 A 是一个 有效括号字符串 。 类似地,可以定义任何有效括号字符串 S 的 嵌套深度 depth(S):

depth("") = 0 depth(C) = 0,其中 C 是单个字符的字符串,且该字符不是 "(" 或者 ")" depth(A + B) = max(depth(A), depth(B)),其中 A 和 B 都是 有效括号字符串 depth("(" + A + ")") = 1 + depth(A),其中 A 是一个 有效括号字符串 例如:""、"()()"、"()(()())" 都是 有效括号字符串(嵌套深度分别为 0、1、2),而 ")(" 、"(()" 都不是 有效括号字符串 。

给你一个 有效括号字符串 s,返回该字符串的 s 嵌套深度 。

示例 1:

输入:s = "(1+(2*3)+((8)/4))+1"
输出:3
解释:数字 8 在嵌套的 3 层括号中。

示例 2:

输入:s = "(1)+((2))+(((3)))"
输出:3

示例 3:

输入:s = "1+(2*3)/(2-1)"
输出:1

示例 4:

输入:s = "1"
输出:0

我的解法

class Solution:
    def maxDepth(self, s: str) -> int:
        stack = []
        res = 0
        tmp = 0
        for i in s:
            if i == '(':
                stack.append('(')
                tmp += 1
                if tmp > res:
                    res = tmp
            if i == ')':
                stack.pop()
                tmp -= 1
        return res

参考解法

class Solution:
    def maxDepth(self, s: str) -> int:
        maxs=0
        counts=0
        for val in s:
            if val=='(':
                counts+=1
            elif val==')':
                counts-=1
            maxs=max(maxs,counts)
        return maxs