当前位置:网站首页>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
边栏推荐
- Using flask_ Whooshalchemyplus Jieba realizes global search of flask
- DVWA exercise 05 file upload file upload
- Keil5 MDK's formatting code tool and adding shortcuts
- Function: find the root of the equation by Newton iterative method
- Report on the double computer experiment of scoring system based on 485 bus
- High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
- 【指针】删除字符串s中的所有空格
- 基于485总线的评分系统双机实验报告
- Get started with Matplotlib drawing
- Keil5-MDK的格式化代码工具及添加快捷方式
猜你喜欢
ucore lab1 系统软件启动过程 实验报告
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
5分钟掌握机器学习鸢尾花逻辑回归分类
四元数---基本概念(转载)
CSAPP homework answers chapter 789
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
ucore lab2 物理内存管理 实验报告
“Hello IC World”
Want to learn how to get started and learn software testing? I'll give you a good chat today
随机推荐
CSAPP家庭作业答案7 8 9章
Keil5 MDK's formatting code tool and adding shortcuts
函数:用牛顿迭代法求方程的根
Don't you even look at such a detailed and comprehensive written software test question?
ucore lab2 物理内存管理 实验报告
"If life is just like the first sight" -- risc-v
Four methods of exchanging the values of a and B
【指针】求二维数组中最大元素的值
后台登录系统,JDBC连接数据库,做小案例练习
[pointer] counts the number of times one string appears in another string
What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
[oiclass] share prizes
Global and Chinese market of pinhole glossmeter 2022-2028: Research Report on technology, participants, trends, market size and share
Pointer -- output all characters in the string in reverse order
ucore lab1 系统软件启动过程 实验报告
Using flask_ Whooshalchemyplus Jieba realizes global search of flask
Réponses aux devoirs du csapp 7 8 9
Global and Chinese markets of cobalt 2022-2028: Research Report on technology, participants, trends, market size and share
Function: find the maximum common divisor and the minimum common multiple of two positive numbers
指针 --按字符串相反次序输出其中的所有字符