当前位置:网站首页>leetcode 20. Valid Parentheses 有效的括号(中等)
leetcode 20. Valid Parentheses 有效的括号(中等)
2022-08-09 22:10:00 【okokabcd】
一、题目大意
标签: 栈和队列
https://leetcode.cn/problems/valid-parentheses
给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
示例 1:
输入:s = “()”
输出:true
示例 2:
输入:s = “()[]{}”
输出:true
示例 3:
输入:s = “(]”
输出:false
示例 4:
输入:s = “([)]”
输出:false
示例 5:
输入:s = “{[]}”
输出:true
提示:
- 1 <= s.length <= 104
- s 仅由括号 ‘()[]{}’ 组成
二、解题思路
思路:括号匹配是典型的使用栈来解决的问题。从左向右遍历,每当遇到左括号便放入栈内,遇到右括号则判断其和栈顶的括号是否是统一类型,是则从栈内取出左括号,否则说明字符不串不合法。
实现:遍历字符串,如果栈为空则将当前字符入栈,如果栈顶字符与当前字符匹配则栈顶字符出栈,不匹配当前字符入栈,遍历结束如果栈为空说明字符串合法。
三、解题方法
3.1 Java实现
public class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (Character c : s.toCharArray()) {
if (isPush(stack, c)) {
stack.push(c);
} else {
stack.pop();
}
}
return stack.isEmpty();
}
private boolean isPush(Stack<Character> stack, Character c) {
if (stack.isEmpty()) {
return true;
}
// {[(
boolean isPop = (stack.peek().equals('[') && c.equals(']') ||
stack.peek().equals('{') && c.equals('}') ||
stack.peek().equals('(') && c.equals(')'));
return !isPop;
}
}
3.2 2014年的实现
public class Solution {
public boolean isValid(String s) {
if (s == null || s.length() == 0) {
return true;
}
Stack<Byte> ps = new Stack<Byte>();
byte[] bArr = s.getBytes();
boolean isValid = true;
for (byte b : bArr) {
if (!isValid) {
break;
}
switch (b) {
case '(':
case '[':
case '{':
ps.push(b);
break;
case ')':
case ']':
case '}':
if (!ps.empty()) {
byte tmpB = ps.pop();
if (tmpB == 40) {
tmpB++;
} else {
tmpB+=2;
}
if (tmpB != b) {
isValid = false;
}
} else {
isValid = false;
}
break;
default:
// do nothing
}
}
if (!ps.empty()) {
isValid = false;
}
return isValid;
}
}
四、总结小记
- 2022/8/9 一场秋雨一场寒
边栏推荐
- 杭电多校-Counting Stickmen-(思维+组合数+容斥)
- Mysql集群 ShardingSphere
- What is the stability of the quantitative trading interface system?
- 【AtomicInteger】常规用法
- 毕昇编译器优化:Lazy Code Motion
- leetcode:286.墙和门
- 【燃】是时候展现真正的实力了!一文看懂2022华为开发者大赛技术亮点
- C 在函数声明前加typedef
- A. Common Prefixes
- R语言ggstatsplot包grouped_ggscatterstats函数可视化分组散点图、并添加假设检验结果(包含样本数、统计量、效应大小及其置信区间、显著性、组间两两比较、贝叶斯假设)
猜你喜欢
反射机制篇
2022-8-9 第六组 输入输出流
OSG笔记:使用setFontResolution设置字体分辨率
Tencent continues to wield the "big knife" to reduce costs and increase efficiency, and free catering benefits for outsourced employees have been cut
17-GuliMall 搭建虚拟域名访问环境
JS中表单操作、addEventListener事件监听器
【Burning】It's time to show your true strength!Understand the technical highlights of the 2022 Huawei Developer Competition in one article
Bi Sheng Compiler Optimization: Lazy Code Motion
chart.js面积图曲线图统计插件
数字与中文大写数字互转(5千万亿亿亿亿以上的数字也支持转换)
随机推荐
torch.distributed多卡/多GPU/分布式DPP(二)——torch.distributed.all_reduce(reduce_mean)&barrier&控制进程执行顺序&随机数种子
友元类和友元函数
Mysql集群 ShardingSphere
全球不用交税的国家,为什么不交
Bi Sheng Compiler Optimization: Lazy Code Motion
Qt 消息机制和事件
shell数组
关于ETL的两种架构(ETL架构和ELT架构)
Redis
PyQt5: Getting Started Tutorial
A1. Prefix Flip (Easy Version)
(转)字符集编码标识符,数字表示字符编码
注意力引导网络用于视网膜图像分割
HUAWEI CLOUD escorts the whole process of "Wandering Ark" for the first time, creating a popular brand
安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育
【面试高频题】可逐步优化的链表高频题
PyQt5:入门使用教程
ArrayList 和 LinkedList 区别
Leetcode 530. 二叉搜索树的最小绝对差
信息系统项目管理师---第十一章项目风险管理历年考题