当前位置:网站首页>LeetCode#20. Valid parentheses
LeetCode#20. Valid parentheses
2022-06-22 21:34:00 【Rufeng ZHHH】
Given one only includes '(',')','{','}','[',']' String s , Determines whether the string is valid .
Valid string needs to meet :
Opening parentheses must be closed with closing parentheses of the same type .
The left parenthesis must be closed in the correct order .
Example 1:
Input :s = "()"
Output :true
Example 2:
Input :s = "()[]{}"
Output :true
Example 3:
Input :s = "(]"
Output :false
Example 4:
Input :s = "([)]"
Output :false
Example 5:
Input :s = "{[]}"
Output :true
author : Power button (LeetCode)
link : Power button
First, through analysis, we know that to satisfy this valid bracket , You must make the three possible parentheses given in the title close . Therefore, it is appropriate to use the stack method to complete this topic . I have previously written articles about stack knowledge Python The order sheet ( list ) Construction stack _ As the wind Zhhh The blog of -CSDN Blog Python The order sheet ( list ) Construction stack https://blog.csdn.net/qq_60926106/article/details/125286009?spm=1001.2014.3001.5502 Ideas as follows :
①. You can do a rough screening first , If the given string ( Here we use s To represent this string )s The length of is equal to 0 Or an odd number , Then you can go back directly False 了 , Because if the parentheses want to form a closure ,s The length of can only be an even number .
②. about s Not for 0 And even numbers , Let's make the following judgment :
1. Traverse the whole s, If the traversed element i (for i in s) by "(" "{" "[" Any one of , We can put their corresponding ")" "}" "]" Pressing stack ( The premise is that we first create a list as our stack ). If i The elements encountered are ")" "}" "]" , We can perform stack out operation , See if the pop-up element is consistent with i equal , Then judge . If equal , Then continue the cycle , If not equal , Go straight back to False that will do .
2. At the end of the cycle , If the element in the stack is 0, Then it means that all the parentheses are closed , Can return True, Otherwise it returns False.
The code is as follows :
class Solution:
def isValid(self, s: str) -> bool:
if len(s)==0 or len(s)%2 != 0:
return False
stack=[]
for i in s:
if i=='(':
stack.append(')')
elif i=='{':
stack.append('}')
elif i=='[':
stack.append(']')
else:
if not stack:
return False
if i==stack.pop():
continue
return False
return stack==[]
边栏推荐
- 74- how to remedy the loss of Oracle to MySQL for this kind of SQL optimization?
- TC397 Flash
- [Jianzhi offer] interview question 44 A digit in a sequence of numbers
- 密码学系列之:PKI的证书格式表示X.509
- Differences between watch, computed and methods
- 92 match for several_ Recognize SQL write example
- 牛客 52次月赛 B牛牛的身高 (思维题 模拟题)
- 513. 找树左下角的值 / 剑指 Offer II 091. 粉刷房子
- [redis]配置文件
- 2022化工自动化控制仪表考试练习题及在线模拟考试
猜你喜欢
![[redis] profile](/img/1c/05c06d59c9efb5983f877822db333c.png)
[redis] profile

【876. 链表的中间结点】

How swiftui simulates the animation effect of view illumination increase

基于C语言开发工资管理系统 课程论文+源码及可执行exe文件
![List of outstanding talents: no crystal vibration, one drag, eight burn and upgrade [chapter]](/img/6c/333bc95fe390234d3d06043e4bded1.png)
List of outstanding talents: no crystal vibration, one drag, eight burn and upgrade [chapter]

300. 最长递增子序列 ●●

第018讲:函数:灵活即强大 | 课后测试题及答案
![[206. reverse linked list]](/img/c8/d83655395379a512c8b67a82dc4cae.png)
[206. reverse linked list]

Fluent system architecture

2022 question bank and simulated examination for work license of main principals of hazardous chemical business units
随机推荐
localStorage、sessionStorage 和 cookie 的区别大总结
[redis] profile
90- review of several recently optimized Oracle databases
csv新增一列
ACM. HJ45 名字的漂亮度 ●●
2022 a special equipment related management (elevator) examination questions and simulation examination
杰理之动态切换 EQ 文件【篇】
微信小程序批量提交审核
第032讲:异常处理:你不可能总是对的 | 课后测试题及答案
75- when left join encounters subquery
第033讲:异常处理:你不可能总是对的2 | 课后测试题及答案
LeetCode#20.有效的括号
Jericho uses DP and DM for IO key detection [chapter]
杰理之硬件上 DACL 输出,DAC 输出左右声道的声音【篇】
分享insert into select遇到的死锁问题(项目实战)
Differences between watch, computed and methods
[160. cross linked list]
牛客 52次月赛 B牛牛的身高 (思维题 模拟题)
Jerry's plug-in 4m flash to view the processing method with a size of only 1m on the PC [chapter]
[138. copy linked list with random pointer]