当前位置:网站首页>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();
}

边栏推荐
猜你喜欢

(1) Software testing theory (0 basic understanding of basic knowledge)

(2) Software Testing Theory (*Key Use Case Method Writing)

HCIP OSPF动态路由协议

vi display line number in buildroot embedded file system

SECURITY DAY06 ( iptables firewall, filter table control, extended matching, typical application of nat table)

buildroot嵌入式文件系统中vi显示行号

HCIP OSPF/MGRE综合实验

(3) Software testing theory (understanding the knowledge of software defects)

window7开启远程桌面功能

Solve win10 installed portal v13 / v15 asked repeatedly to restart problem.
随机推荐
使用路由器DDNS功能+动态公网IP实现外网访问(花生壳)
(2) Software Testing Theory (*Key Use Case Method Writing)
arcgis填坑_4
WiFi Deauth 攻击演示分析 // mdk4 // aireplay-ng// Kali // wireshark //
训练分类器
HCIP BGP建邻实验
iptables 使用脚本来管理规则
MySQl进阶之索引结构
智能合约 ——— app评分合约
iptables nat
文本三剑客——sed 修改、替换
Arcgis小工具_实现重叠分析
空间点模式方法_一阶效应和二阶效应
Windos10专业版开启远程桌面协助
ansible batch install zabbix-agent
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-27
HPC platform building
查看可执行文件依赖的库ldd
ETCD集群故障应急恢复-从snapshot恢复
CLUSTER DAY02 (Keepalived Hot Standby, Keepalived+LVS, HAProxy Server)