当前位置:网站首页>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();
}
边栏推荐
- Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-20
- AUTOMATION DAY06 (Ansible Advanced, Ansible Role)
- Basic use of Slurm
- Threatless Technology-TVD Daily Vulnerability Intelligence-2022-8-3
- ansible批量安装zabbix-agent
- 【LeetCode】851.喧闹与富有(思路+题解)
- uboot code analysis 1: find the main line according to the purpose
- HCIA实验
- HCIP BGP建邻、联邦、汇总实验
- HCIP OSPF/MGRE综合实验
猜你喜欢
TCP 三次握手、四次断开
HCIP-BGP的选路实验
Solve win10 installed portal v13 / v15 asked repeatedly to restart problem.
查看CPU和其他硬件温度的软件
lvm multi-disk mount, combined use
SECURITY DAY04( Prometheus服务器 、 Prometheus被监控端 、 Grafana 、 监控数据库)
CLUSTER DAY01 (Introduction to cluster and LVS, LVS-NAT cluster, LVS-DR cluster)
ETCD集群故障应急恢复-本地数据可用
解决8080端口被占用问题
使用路由器DDNS功能+动态公网IP实现外网访问(花生壳)
随机推荐
SECURITY DAY01(监控概述 、 Zabbix基础 、 Zabbix监控服 )
xx is not recognized as internal or external command
日志收集分析器(ELK)
照片的35x45,300dpi怎么弄
SECURITY DAY05 (Kali system, scanning and caught, SSH basic protection, service SECURITY)
训练分类器
ssh中的密码登录和密钥登录
(1) Software testing theory (0 basic understanding of basic knowledge)
SECURITY DAY06 ( iptables firewall, filter table control, extended matching, typical application of nat table)
WiFi Deauth 攻击演示分析 // mdk4 // aireplay-ng// Kali // wireshark //
HCIP MPLS/BGP综合实验
AUTOMATION DAY06 (Ansible Advanced, Ansible Role)
文本三剑客——awk 截取+过滤+统计
AUTOMATION DAY07 (Ansible Vault, ordinary users use ansible)
arcgis填坑_3
SECURITY DAY02 (Zabbix alarm mechanism, Zabbix advanced operation and monitoring case)
cloudreve使用体验
iptables入门
View the library ldd that the executable depends on
buildroot嵌入式文件系统中vi显示行号