当前位置:网站首页>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 .
边栏推荐
- Leetcode 笔记 20. 有效的括号
- 数据流通交易场景下数据质量综合管理体系与技术框架研究
- Hoops exchange helps hybrid computational fluid dynamics software build 3D format import and read function | customer case
- GOM and GEE engine black screen does not display the interface, and the solution of equipping map monsters
- Nodejs starts mqtt service with an error schemaerror: expected 'schema' to be an object or Boolean problem solving
- 数据库工具对决:HeidiSQL 与 Navicat
- [paper notes] - target attitude estimation Epro PNP 2022 CVPR
- 进程与线程
- 基于数据要素流通视角的数据溯源研究进展
- Research progress of data traceability based on the perspective of data element circulation
猜你喜欢

Preparation of bovine serum albumin modified by low molecular weight protamine lmwp/peg-1900 on the surface of albumin nanoparticles

DC-6 -- vulnhub range

分布式事务 :可靠消息最终一致性方案

对比7种分布式事务方案,还是偏爱阿里开源的Seata(原理+实战)

JVM Tri Color marking and read-write barrier

Redis夺命十二问,你能扛到第几问?

Comparing the seven distributed transaction schemes, I prefer Alibaba's open source Seata (principle + Practice)

Sorting out the encapsulation classes of control elements in appium

MWEC:一种基于多语义词向量的中文新词发现方法

Study on bovine serum protein modified phenolic acids and alkaloids small molecules / coupled microspheres protein / bovine erythrocyte SOD
随机推荐
Leetcode 笔记 20. 有效的括号
测试左移和测试右移的概念
基于SEIR模型的网络医疗众筹传播建模与仿真分析
HNOI2012矿场搭建
P4047 [jsoi2010] tribal Division
DC-6--vulnhub靶场
GOM and GEE engine black screen does not display the interface, and the solution of equipping map monsters
sql语句练习
[calculate the number of times that one string is equal to another string]
Wechat applet for loop
DC-6 -- vulnhub range
Super super super realistic digital people! Keep you on the air 24 hours a day
Sorting out the encapsulation classes of control elements in appium
How much data can a list store?
The way to understand JS: the principle of object.call and object.create() inheritance
分布式事务和Seata的AT模式原理
[contents] mqtt, nodejs projects
Research on text classification of e-commerce comments based on mffmb
[GOM engine] script setting method for dummy configuration
实战演练 | 查找在给定时间范围内购买超过 N 件商品的客户