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

边栏推荐
- Express
- 数据库多表链接的查询方式
- 移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
- Pointer -- output all characters in the string in reverse order
- Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
- 1. Payment system
- DVWA exercise 05 file upload file upload
- [Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases
- Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
- [issue 18] share a Netease go experience
猜你喜欢

四元数---基本概念(转载)

Summary of thread implementation

Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class

Login the system in the background, connect the database with JDBC, and do small case exercises

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

Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator

5 minutes to master machine learning iris logical regression classification

Install and run tensorflow object detection API video object recognition system of Google open source

Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?

移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
随机推荐
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
Thinking about three cups of tea
数字电路基础(二)逻辑代数
Four methods of exchanging the values of a and B
5 minutes to master machine learning iris logical regression classification
Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class
Cc36 different subsequences
Database monitoring SQL execution
MySQL development - advanced query - take a good look at how it suits you
【指针】求二维数组中最大元素的值
What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
Matplotlib绘图快速入门
Global and Chinese market of RF shielding room 2022-2028: Research Report on technology, participants, trends, market size and share
四元数---基本概念(转载)
[HCIA continuous update] advanced features of routing
Why can swing implement a form program by inheriting the JFrame class?
Using flask_ Whooshalchemyplus Jieba realizes global search of flask
What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?
Pointers: maximum, minimum, and average
Function: find the maximum common divisor and the minimum common multiple of two positive numbers