当前位置:网站首页>Maximum nesting depth of parentheses in leetcode simple questions
Maximum nesting depth of parentheses in leetcode simple questions
2022-07-06 15:04:00 【·Starry Sea】
subject
If the string meets one of the following conditions , It can be called Valid bracket string (valid parentheses string, I could just write it as VPS):
The string is an empty string “”, Or a not for “(” or “)” The single character of .
The string can be written as AB(A And B String connection ), among A and B All are Valid bracket string .
The string can be written as (A), among A It's a Valid bracket string .
Similarly , You can define any valid parenthesis string S Of Nesting depth depth(S):
depth("") = 0
depth = 0, among C Is a single character string , And the character is not “(” perhaps “)”
depth(A + B) = max(depth(A), depth(B)), among A and B All are Valid bracket string
depth("(" + A + “)”) = 1 + depth(A), among A It's a Valid bracket string
for example :""、"()()"、"()(()())" All are Valid bracket string ( The nesting depth is 0、1、2), and “)(” 、"(()" Are not Valid bracket string .
To give you one Valid bracket string s, Returns the of the string s Nesting depth .
Example 1:
Input :s = “(1+(23)+((8)/4))+1"
Output :3
explain : Numbers 8 In the nested 3 Layer in parentheses .
Example 2:
Input :s = “(1)+((2))+(((3)))”
Output :3
Example 3:
Input :s = "1+(23)/(2-1)”
Output :1
Example 4:
Input :s = “1”
Output :0
Tips :
1 <= s.length <= 100
s By digital 0-9 And character ‘+’、’-’、’*’、’/’、’(’、’)’ form
Title Data guarantee bracket expression s yes Valid parenthesis expression
source : Power button (LeetCode)
Their thinking
Traversing the entire string , Add parentheses on one side only ’(‘ or ’)' And update the maximum depth . In addition, because the given string in the title has been guaranteed to be a valid kueha expression , So we don't need to update the maximum depth every time we accumulate parentheses , The maximum depth can be updated only when the opposite bracket is encountered .
class Solution:
def maxDepth(self, s: str) -> int:
MAX=0
count=0
for i in s:
if i=='(':
count+=1
elif i==')':
if MAX<count:
MAX=count
count-=1
return MAX

边栏推荐
- Global and Chinese market of maleic acid modified rosin esters 2022-2028: Research Report on technology, participants, trends, market size and share
- [issue 18] share a Netease go experience
- 5分钟掌握机器学习鸢尾花逻辑回归分类
- 函数:求1-1/2+1/3-1/4+1/5-1/6+1/7-…+1/n
- Wang Shuang's detailed learning notes of assembly language II: registers
- Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
- 【指针】使用插入排序法将n个数从小到大进行排列
- 【指针】求字符串的长度
- Using flask_ Whooshalchemyplus Jieba realizes global search of flask
- 刷视频的功夫,不如看看这些面试题你掌握了没有,慢慢积累月入过万不是梦。
猜你喜欢

Database monitoring SQL execution

Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class

数字电路基础(二)逻辑代数

Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers

The common methods of servlet context, session and request objects and the scope of storing data in servlet.

Es full text index
![Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)](/img/85/d6b196f22b60ad5003f73eb8d8a908.png)
Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)

MySQL development - advanced query - take a good look at how it suits you

Wang Shuang's detailed notes on assembly language learning I: basic knowledge

Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
随机推荐
【指针】求二维数组中最大元素的值
Function: find the root of the equation by Newton iterative method
“Hello IC World”
四元数---基本概念(转载)
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
Flash implements forced login
ucore lab1 系统软件启动过程 实验报告
Fundamentals of digital circuits (III) encoder and decoder
函数:求1-1/2+1/3-1/4+1/5-1/6+1/7-…+1/n
Get started with Matplotlib drawing
数字电路基础(二)逻辑代数
关于交换a和b的值的四种方法
Detailed introduction to dynamic programming (with examples)
基于485总线的评分系统双机实验报告
{1,2,3,2,5} duplicate checking problem
线程的实现方式总结
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
Report on the double computer experiment of scoring system based on 485 bus
数字电路基础(三)编码器和译码器
Function: string storage in reverse order