当前位置:网站首页>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
- Install and run tensorflow object detection API video object recognition system of Google open source
- 浙大版《C语言程序设计实验与习题指导(第3版)》题目集
- 移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
- 5分钟掌握机器学习鸢尾花逻辑回归分类
- “Hello IC World”
- 数据库多表链接的查询方式
- 基于485总线的评分系统双机实验报告
- Pointers: maximum, minimum, and average
猜你喜欢

5 minutes to master machine learning iris logical regression classification

Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)

Want to learn how to get started and learn software testing? I'll give you a good chat today

Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025

Description of Vos storage space, bandwidth occupation and PPS requirements

What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?

移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
![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)
![[HCIA continuous update] advanced features of routing](/img/05/a9ed32ec8c19b236355d48f7c2ad80.jpg)
[HCIA continuous update] advanced features of routing

Keil5 MDK's formatting code tool and adding shortcuts
随机推荐
ES全文索引
[pointer] the array is stored in reverse order and output
ucore lab2 物理内存管理 实验报告
移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
Function: find the maximum common divisor and the minimum common multiple of two positive numbers
Interview Essentials: what is the mysterious framework asking?
指针 --按字符串相反次序输出其中的所有字符
Expanded polystyrene (EPS) global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
Pointeurs: maximum, minimum et moyenne
函数:求方程的根
[pointer] solve the last person left
数字电路基础(二)逻辑代数
Global and Chinese market of goat milk powder 2022-2028: Research Report on technology, participants, trends, market size and share
Four methods of exchanging the values of a and B
“Hello IC World”
Oracle foundation and system table
Global and Chinese markets of PIM analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets of Iam security services 2022-2028: Research Report on technology, participants, trends, market size and share
ucore lab8 文件系统 实验报告
Don't you even look at such a detailed and comprehensive written software test question?