当前位置:网站首页>【LeetCode】20.有效的括号
【LeetCode】20.有效的括号
2022-08-02 02:40:00 【酥酥~】
题目
给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
示例 1:
输入:s = “()”
输出:true
示例 2:
输入:s = “()[]{}”
输出:true
示例 3:
输入:s = “(]”
输出:false
示例 4:
输入:s = “([)]”
输出:false
示例 5:
输入:s = “{[]}”
输出:true
提示:
1 <= s.length <= 104
s 仅由括号 ‘()[]{}’ 组成
题解
使用栈来进行
使用map存储括号对
class Solution {
public:
bool isValid(string s) {
int len = s.length();
if(len%2==1)
return false;
unordered_map<char,char> pairs={
{
')','('},
{
']','['},
{
'}','{'}};
stack<char> mystack;
for(char ch:s)
{
if(pairs.count(ch))
{
if(mystack.empty() || mystack.top() != pairs[ch])
{
return false;
}
mystack.pop();
}
else
{
mystack.push(ch);
}
}
return mystack.empty();
}
};
边栏推荐
- C#测试项目中属性的用法
- Flask之路由(app.route)详解
- 使用self和_(下划线)的区别
- 【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
- 【web】Understanding Cookie and Session Mechanism
- 记一次gorm事务及调试解决mysql死锁
- [Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
- leetcode / anagram in string - some permutation of s1 string is a substring of s2
- 使用docker安装mysql
- 2022牛客多校四_G M
猜你喜欢

Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?

Oracle数据类型介绍

NAS和私有云盘的区别?1篇文章说清楚

字典常用方法

Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案

Power button 1374. Generate each character string is an odd number

nacos startup error, the database has been configured, stand-alone startup

【web】理解 Cookie 和 Session 机制

Chopper webshell feature analysis

240...循迹
随机推荐
1688以图搜货
Oracle19c安装图文教程
Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
忽晴忽雨
analog IC layout-Design for reliability
The state status is displayed incorrectly after the openGauss switch
isa指针使用详情
2022年NPDP考完多久出成绩?怎么查询?
2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
analog IC layout
简单的页面跳转活动
2022 NPDP take an examination of how the results?How to query?
微服务:微智能在软件系统的简述
esp32经典蓝牙和单片机连接,,,手机蓝牙作为主机
mockjs生成假数据的基本使用
nacos startup error, the database has been configured, stand-alone startup
最大层内元素和
AI目标分割能力,无需绿幕即可实现快速视频抠图
Lombok
【web】理解 Cookie 和 Session 机制