当前位置:网站首页>Top20 bracket matching
Top20 bracket matching
2022-08-11 07:08:00 【geekmice】
题目描述
给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效.
有效字符串需满足:
左括号必须用相同类型的右括号闭合.
左括号必须以正确的顺序闭合.
示例 1:
输入:s = “()”
输出:true
示例 2:
输入:s = “()[]{}”
输出:true
示例 3:
输入:s = “(]”
输出:false
示例 4:
输入:s = “([)]”
输出:false
示例 5:
输入:s = “{[]}”
输出:true
解决方案
public static boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for (char c : s.toCharArray()) {
if (c == '(') {
stack.push(')');
} else if (c == '[') {
stack.push(']');
} else if (c == '{') {
stack.push('}');
} else if (stack.isEmpty() || c != stack.pop()) {
return false;
}
}
return stack.isEmpty();
}

边栏推荐
- 【LeetCode】1036. 逃离大迷宫(思路+题解)压缩矩阵+BFS
- Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-30
- MoreFileRename batch file renaming tool
- Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-31
- No threat of science and technology - TVD vulnerability information daily - 2022-8-4
- window10吐槽
- 日志收集分析器(ELK)
- detectron2,手把手教你训练mask_rcnn
- China Mobile Communications Group Co., Ltd.: Business Power of Attorney
- ramdisk实践1:将根文件系统集成到内核中
猜你喜欢

解决win10安装portal v13/v15要求反复重启问题。

局域网文件传输

CLUSTER DAY03 (Ceph overview, the deployment of Ceph CLUSTER, Ceph block storage)

文本三剑客——grep过滤

The ramdisk practice 1: the root file system integrated into the kernel

slurm集群搭建

MoreFileRename批量文件改名工具

buildroot setup dhcp

Solve the problem that port 8080 is occupied

Basic use of Slurm
随机推荐
cloudreve使用体验
空间点模式方法_一阶效应和二阶效应
Two hundred questions in C language (0 basic continuous update) (1~5)
iptables的状态
HCIP OSPF动态路由协议
CLUSTER DAY01 (Introduction to cluster and LVS, LVS-NAT cluster, LVS-DR cluster)
SECURITY DAY04 (Prometheus server, Prometheus monitored terminal, Grafana, monitoring database)
iptables 流量统计
kill 命令
HCIP-生成树(802.1D ,标准生成树/802.1W : RSTP 快速生成树/802.1S : MST 多生成树)
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-20
HCIP-BGP的选路实验
华为防火墙-3-应用过滤
SECURITY DAY05 (Kali system, scanning and caught, SSH basic protection, service SECURITY)
iptables 使用脚本来管理规则
Numpy_备注
View the library ldd that the executable depends on
xx is not recognized as internal or external command
八股文之mysql
【LeetCode】306.累加数(思路+题解)