当前位置:网站首页>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();
}
边栏推荐
- slurm cluster construction
- 八股文之并发编程
- Threatless Technology-TVD Daily Vulnerability Intelligence-2022-8-2
- pytorch下tensorboard可视化深坑
- HPC platform building
- 八股文之mysql
- FusionCompute8.0.0 实验(2)虚拟机创建
- 文本三剑客——awk 截取+过滤+统计
- lvm multi-disk mount, combined use
- Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-22
猜你喜欢
(2) Software Testing Theory (*Key Use Case Method Writing)
解决win10安装portal v13/v15要求反复重启问题。
八股文之mysql
文本三剑客——grep过滤
文本三剑客——sed 修改、替换
ETCD容器化搭建集群
MoreFileRename batch file renaming tool
The ramdisk practice 1: the root file system integrated into the kernel
HCIP-生成树(802.1D ,标准生成树/802.1W : RSTP 快速生成树/802.1S : MST 多生成树)
HCIP MPLS/BGP综合实验
随机推荐
MySQl进阶之索引结构
Slurm基本使用
lvm multi-disk mount, combined use
(3) Software testing theory (understanding the knowledge of software defects)
HCIP OSPF动态路由协议
HCIP MPLS/BGP综合实验
vi display line number in buildroot embedded file system
window10吐槽
HCIP MGRE\OSPF综合实验
SECURITY DAY05(Kali系统 、 扫描与抓包 、 SSH基本防护、服务安全 )
八股文之jvm
xx is not recognized as internal or external command
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-27
AUTOMATION DAY07( Ansible Vault 、 普通用户使用ansible)
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-26
训练分类器
华为防火墙-5-NAT
slurm集群搭建
uboot sets the default bootdelay
buildroot嵌入式文件系统中vi显示行号