当前位置:网站首页>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
边栏推荐
猜你喜欢
1.支付系统
ES全文索引
移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
CSAPP家庭作業答案7 8 9章
China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
Logstack introduction and deployment -- elasticstack (elk) work notes 019
四元数---基本概念(转载)
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
随机推荐
5 minutes to master machine learning iris logical regression classification
CSAPP家庭作業答案7 8 9章
函数:求方程的根
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
Function: find the root of the equation by Newton iterative method
Pointer -- output all characters in the string in reverse order
[pointer] octal to decimal
Install and run tensorflow object detection API video object recognition system of Google open source
What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
Keil5 MDK's formatting code tool and adding shortcuts
Stc-b learning board buzzer plays music 2.0
Function: string storage in reverse order
Global and Chinese market of maleic acid modified rosin esters 2022-2028: Research Report on technology, participants, trends, market size and share
With 27K successful entry ByteDance, this "software testing interview notes" has benefited me for life
指針:最大值、最小值和平均值
Four methods of exchanging the values of a and B
How to transform functional testing into automated testing?
Global and Chinese markets for GaN on diamond semiconductor substrates 2022-2028: Research Report on technology, participants, trends, market size and share
Quaternion -- basic concepts (Reprint)