当前位置:网站首页>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 .
边栏推荐
- Flask send verification code logic
- Verilog grammar basics HDL bits training 05
- The way to understand JS: the principle of object.call and object.create() inheritance
- Find the single dog (Li Kou 260)
- Solve page refresh without attaching data
- 从另一个角度告诉你单元测试的意义
- 2022/7/19 exam summary
- 基于数据要素流通视角的数据溯源研究进展
- Use localdate class to complete calendar design
- Rotate K strings to the left (details)
猜你喜欢

Nest. JS uses express but not completely

Find and locate commands
![[untitled] how to realize pluggable configuration?](/img/48/673a2d63e11679f1e75ca3ca7ed6a9.jpg)
[untitled] how to realize pluggable configuration?

Super super super realistic digital people! Keep you on the air 24 hours a day

基于网络分析和文本挖掘的意见领袖影响力研究

8个小妙招-数据库性能优化,yyds~

Quick start sequence table chain table

Verilog语法基础HDL Bits训练 06

LCA three postures (multiplication, tarjan+ joint search set, tree chain dissection)

基于数据要素流通视角的数据溯源研究进展
随机推荐
Verilog语法基础HDL Bits训练 05
Find the single dog (Li Kou 260)
[redis] ② redis general command; Why is redis so fast?; Redis data type
Wechat applet dynamic style | parameter transfer
2022/7/25 exam summary
Shardingsphere data slicing
【计算一个字符串和另一个字符串相等的次数】
向左旋转k个字符串(细节)
How to open the Internet and ask friends to play together?
Solve page refresh without attaching data
12. Neural network model
After seven years of testing, the interview with Huawei finally negotiated a salary of 10000. HR said that I didn't respect Huawei and they didn't have such a low salary position~
ShardingSphere数据分片
DC-6 -- vulnhub range
Four characteristics and isolation level of MySQL transactions
你还在掐表算时间复杂度?
letfaw
Use localdate class to complete calendar design
D3D计算着色器入门
sql(基础二)