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

边栏推荐
- With 27K successful entry ByteDance, this "software testing interview notes" has benefited me for life
- “人生若只如初见”——RISC-V
- 移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
- servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
- 【指针】求二维数组中最大元素的值
- Wang Shuang's detailed notes on assembly language learning I: basic knowledge
- [issue 18] share a Netease go experience
- 【指针】删除字符串s中的所有空格
- Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
- 函数:求两个正数的最大公约数和最小公倍
猜你喜欢

Summary of thread implementation

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

Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class

Express

Fundamentals of digital circuits (II) logic algebra

HackTheBox-Emdee five for life

5 minutes to master machine learning iris logical regression classification

ES全文索引

Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008

C language do while loop classic Level 2 questions
随机推荐
Keil5-MDK的格式化代码工具及添加快捷方式
Global and Chinese market of maleic acid modified rosin esters 2022-2028: Research Report on technology, participants, trends, market size and share
MySQL development - advanced query - take a good look at how it suits you
CSAPP homework answers chapter 789
How to transform functional testing into automated testing?
[pointer] find the value of the largest element in the two-dimensional array
Logstack introduction and deployment -- elasticstack (elk) work notes 019
Want to learn how to get started and learn software testing? I'll give you a good chat today
With 27K successful entry ByteDance, this "software testing interview notes" has benefited me for life
Flash implements forced login
【指针】求字符串的长度
How to learn automated testing in 2022? This article tells you
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
Réponses aux devoirs du csapp 7 8 9
Common Oracle commands
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
CSAPP家庭作业答案7 8 9章