当前位置:网站首页>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
边栏推荐
- Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
- 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
- Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025
- 王爽汇编语言详细学习笔记二:寄存器
- Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
- Global and Chinese market for antiviral coatings 2022-2028: Research Report on technology, participants, trends, market size and share
- Opencv recognition of face in image
- What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?
- MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
猜你喜欢
How to transform functional testing into automated testing?
ucore lab7 同步互斥 实验报告
CSAPP homework answers chapter 789
Es full text index
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)
What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
Get started with Matplotlib drawing
随机推荐
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
Why can swing implement a form program by inheriting the JFrame class?
【指针】数组逆序重新存放后并输出
Fundamentals of digital circuits (I) number system and code system
Don't you even look at such a detailed and comprehensive written software test question?
【指针】删除字符串s中的所有空格
CSAPP家庭作業答案7 8 9章
Software testing interview summary - common interview questions
数字电路基础(四) 数据分配器、数据选择器和数值比较器
Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class
【指针】查找最大的字符串
【指针】统计一字符串在另一个字符串中出现的次数
"If life is just like the first sight" -- risc-v
China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
JDBC 的四种连接方式 直接上代码
About the garbled code problem of superstar script
Function: find 1-1/2+1/3-1/4+1/5-1/6+1/7-... +1/n
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
Install and run tensorflow object detection API video object recognition system of Google open source
The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?