当前位置:网站首页>Leetcode notes 20. valid parentheses
Leetcode notes 20. valid parentheses
2022-07-26 00:39:00 【Lyrig~】
Leetcode note 20. Valid parenthesis
Title Description
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
Tips :
1 <= s.length <= 104
s Brackets only ‘()[]{}’ form
Ideas
This is the classic stack subject , The essence of valid parentheses , That is, at the corresponding level , Correspondence between brackets , If there are no brackets corresponding or different brackets at the same level , Is determined to be invalid brackets . This is related to stack The behavior of entering and leaving the stack completely conforms to .
Code
class Solution {
public:
bool isValid(string s) {
stack<char> front;
for(char i : s)
{
if(i == '(' || i == '[' || i == '{')
{
front.push(i);
}
else{
if(front.empty())
{
return false;
}
char top = front.top();
front.pop();
if((i == ')' && top != '(') || (i == ']' && top != '[') || (i == '}' && top != '{'))
{
return false;
}
}
}
if(front.empty())
return true;
else return false;
}
};
Be careful
- It's going on stack Of top Before extraction , Be sure to judge empty situation , Otherwise, it is extremely easy to appear runtime error The situation of .
- Pay attention to special situations , That is, there is only one bracket , This special case of the very underworld .
边栏推荐
- Research on the influence of opinion leaders based on network analysis and text mining
- Study on bovine serum protein modified phenolic acids and alkaloids small molecules / coupled microspheres protein / bovine erythrocyte SOD
- Research on text classification of e-commerce comments based on mffmb
- YOLOV3
- @The difference between requestparam and @pathvariable annotations
- Tid-mop: a comprehensive framework for security management and control under the scenario of data exchange
- Solve page refresh without attaching data
- GOM and GEE engine black screen does not display the interface, and the solution of equipping map monsters
- 2022/7/24 examination summary
- 使用CMake编译OpenFoam求解器
猜你喜欢

为了拿捏 Redis 数据结构,我画了 40 张图(完整版)

找出单身狗(力扣260)

C#从入门到精通(三)

Hcip day 12

In order to grasp the redis data structure, I drew 40 pictures (full version)

向左旋转k个字符串(细节)

Nodejs learning
![[redis] ② redis general command; Why is redis so fast?; Redis data type](/img/72/aaa90d5411b8b20b15a7f87b98bd27.png)
[redis] ② redis general command; Why is redis so fast?; Redis data type
![[redis] ③ data elimination strategy, pipeline command, publish and subscribe](/img/80/7caeb24380ea026aa8153f2169dfdd.png)
[redis] ③ data elimination strategy, pipeline command, publish and subscribe

Distributed transactions: the final consistency scheme of reliable messages
随机推荐
How to open the Internet and ask friends to play together?
DC-6 -- vulnhub range
Hcip - republish
使用LocalDate类完成日历设计
OAuth2和JWT
Leetcode 笔记 121. 买卖股票的最佳时机
sql语句练习
Nodejs starts mqtt service with an error schemaerror: expected 'schema' to be an object or Boolean problem solving
GOM and GEE engine black screen does not display the interface, and the solution of equipping map monsters
Data driven DDT for automated testing
快速入门顺序表链表
YOLOV2 YOLO9000
What is Web3 game?
Comparing the seven distributed transaction schemes, I prefer Alibaba's open source Seata (principle + Practice)
数据库工具对决:HeidiSQL 与 Navicat
hyperf使用之curd
P4047 [jsoi2010] tribal Division
【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源
Super super super realistic digital people! Keep you on the air 24 hours a day
本地电脑架设传奇怎么开外网叫朋友一起玩?