当前位置:网站首页>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

边栏推荐
- Thinking about three cups of tea
- 【指针】求字符串的长度
- Report on the double computer experiment of scoring system based on 485 bus
- [Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases
- 指針:最大值、最小值和平均值
- Flash implements forced login
- Opencv recognition of face in image
- [pointer] solve the last person left
- Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
- Interview Essentials: what is the mysterious framework asking?
猜你喜欢

后台登录系统,JDBC连接数据库,做小案例练习

“人生若只如初见”——RISC-V

ucore lab8 文件系统 实验报告

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

Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class

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

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

China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '

ucore lab1 系统软件启动过程 实验报告

Get started with Matplotlib drawing
随机推荐
Investment should be calm
[pointer] counts the number of times one string appears in another string
{1,2,3,2,5} duplicate checking problem
Differences between select, poll and epoll in i/o multiplexing
Pointers: maximum, minimum, and average
“Hello IC World”
Function: find 1-1/2+1/3-1/4+1/5-1/6+1/7-... +1/n
What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
[pointer] find the length of the string
Global and Chinese market of DVD recorders 2022-2028: Research Report on technology, participants, trends, market size and share
Don't you even look at such a detailed and comprehensive written software test question?
Global and Chinese markets for GaN on diamond semiconductor substrates 2022-2028: Research Report on technology, participants, trends, market size and share
【指针】八进制转换为十进制
Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
5分钟掌握机器学习鸢尾花逻辑回归分类
Global and Chinese market of RF shielding room 2022-2028: Research Report on technology, participants, trends, market size and share
Install and run tensorflow object detection API video object recognition system of Google open source
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
Pointer -- eliminate all numbers in the string