当前位置:网站首页>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 一场秋雨一场寒
边栏推荐
猜你喜欢

Tencent continues to wield the "big knife" to reduce costs and increase efficiency, and free catering benefits for outsourced employees have been cut

Good future, want to be a second new Oriental

leetcode:332. 重新安排行程

Core Data浅谈系列之五 : 在UITableView中展示

Qt message mechanism and events

18-GuliMall 压力测试与性能监控

五分钟商学院(基础---商业篇)

Leetcode.25 K个一组翻转链表(模拟/递归)

leetcode:321. 拼接最大数

为什么刀具数据库无法打开?
随机推荐
Analyze the Add() method in Fragment management from the source code
setter与getter访问器属性——数据驱动显示
leetcode:319. 灯泡开关
Tencent continues to wield the "big knife" to reduce costs and increase efficiency, and free catering benefits for outsourced employees have been cut
mysql 、pg 查询日期处理
HBuilder X 不能运行到内置终端
毕昇编译器优化:Lazy Code Motion
Users should clearly know that quantitative trading is not a simple procedure
(转)字符集编码标识符,数字表示字符编码
OFDM 十六讲 7 - Inter-Symbol-Interference
Install win7 virtual machine in Vmware and related simple knowledge
Pytorch分布式训练/多卡训练DDP——模型初始化(torch.distribute 与 DDP的区别)
tiup cluster template
LeetCode_2632_字符串压缩
R语言patchwork包将多个可视化结果组合起来、使用plot_annotation函数以及tag_level参数将组合图用大写字母进行顺序编码、为组合图的标签添加自定义前缀信息
高数_复习_第4章:向量代数和空间解析几何
B. Neighbor Grid
安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育
【TS技术课堂】时间序列预测
继承关系下构造方法的访问特点