当前位置:网站首页>Top20括号匹配
Top20括号匹配
2022-08-11 05:35: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();
}
边栏推荐
- kill 命令
- Deep Learning Matlab Toolbox Code Comments
- buildroot设置dhcp
- Record a Makefile just written
- 华为防火墙会话 session table
- 训练分类器
- slurm cluster construction
- SECURITY DAY03 (one-click deployment of zabbix)
- (2) Software Testing Theory (*Key Use Case Method Writing)
- (1) Software testing theory (0 basic understanding of basic knowledge)
猜你喜欢
ETCD cluster fault emergency recovery - local data is available
SECURITY DAY04 (Prometheus server, Prometheus monitored terminal, Grafana, monitoring database)
CLUSTER DAY03( Ceph概述 、 部署Ceph集群 、 Ceph块存储)
Slurm基本使用
Raspberry Pi set static IP address
arcgis填坑_3
Solve the problem that port 8080 is occupied
arcgis填坑_2
解决8080端口被占用问题
(1) Software testing theory (0 basic understanding of basic knowledge)
随机推荐
iptables 流量统计
ETCD集群故障应急恢复-从snapshot恢复
ssh中的密码登录和密钥登录
Basic use of Slurm
华为防火墙-6
lvm multi-disk mount, combined use
lvm 多盘挂载,合并使用
华为防火墙会话 session table
deepin v20.6+cuda+cudnn+anaconda(miniconda)
HCIA知识复习
文本三剑客——sed 修改、替换
HCIP MPLS/BGP综合实验
iptables入门
ramdisk实践1:将根文件系统集成到内核中
Project Notes - Random 2
iptables的状态
Record a Makefile just written
SECURITY DAY05 (Kali system, scanning and caught, SSH basic protection, service SECURITY)
xx is not recognized as internal or external command
AUTOMATION DAY07 (Ansible Vault, ordinary users use ansible)