当前位置:网站首页>【Hot100】20. 有效的括号
【Hot100】20. 有效的括号
2022-07-01 15:49:00 【王六六的IT日常】
- 使用map
private static final Map<Character,Character> map = new HashMap<Character,Character>(){
{
put('{','}'); put('[',']'); put('(',')'); put('?','?');
}};
public boolean isValid(String s) {
if(s.length() > 0 && !map.containsKey(s.charAt(0))) return false;
LinkedList<Character> stack = new LinkedList<Character>() {
{
add('?'); }};
for(Character c : s.toCharArray()){
if(map.containsKey(c)) stack.addLast(c);
else if(map.get(stack.removeLast()) != c) return false;
}
return stack.size() == 1;
}
- 不使用map,用栈
class Solution {
public boolean isValid(String s) {
if(s.isEmpty()){
return true;
}
Stack<Character> stack=new Stack<>();
for(char c:s.toCharArray()){
if(c=='(')
stack.push(')');
else if(c=='{')
stack.push('}');
else if(c=='[')
stack.push(']');
else if(stack.empty()||c!=stack.pop())
return false;
}
return stack.empty();
}
}
- 双队列
class Solution {
public boolean isValid(String s) {
Deque<Character> deque = new LinkedList<>();
char ch;
for (int i = 0; i < s.length(); i++) {
ch = s.charAt(i);
//碰到左括号,就把相应的右括号入栈
if (ch == '(') {
deque.push(')');
}else if (ch == '{') {
deque.push('}');
}else if (ch == '[') {
deque.push(']');
} else if (deque.isEmpty() || deque.peek() != ch) {
return false;
}else {
//如果是右括号判断是否和栈顶元素匹配
deque.pop();
}
}
//最后判断栈中元素是否匹配
return deque.isEmpty();
}
}
在迭代过程中,提前发现不符合的括号并且返回,提升算法效率。
边栏推荐
- Deep operator overloading (2)
- AVL balanced binary search tree
- vscode 查找 替换 一个文件夹下所有文件的数据
- 你TM到底几点下班?!!!
- Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all
- Does 1.5.1 in Seata support mysql8?
- 药品溯源夯实安全大堤
- 三星率先投产3nm芯片,上海应届硕士生可直接落户,南开成立芯片科学中心,今日更多大新闻在此...
- Nuxt.js数据预取
- How to adjust the size of computer photos to what you want
猜你喜欢

工厂高精度定位管理系统,数字化安全生产管理

AVL balanced binary search tree

Pnas: brain and behavior changes of social anxiety patients with empathic embarrassment

Please, stop painting star! This has nothing to do with patriotism!

STM32ADC模拟/数字转换详解

Reading notes of top performance version 2 (V) -- file system monitoring
![[IDM] IDM downloader installation](/img/2b/baf8852b422c1c4a18e9c60de864e5.png)
[IDM] IDM downloader installation

Introduction to RT thread env tool (learning notes)

MySQL高级篇4

使用腾讯云搭建图床服务
随机推荐
Preorder, inorder, follow-up of binary tree (non recursive version)
使用腾讯云搭建图床服务
Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
For the sustainable development of software testing, we must learn to knock code?
微服务追踪SQL(支持Isto管控下的gorm查询追踪)
药品溯源夯实安全大堤
【显存优化】深度学习显存优化方法
Pico,是要拯救还是带偏消费级VR?
What is the forkjoin framework in the concurrent programming series?
Comment win11 définit - il les permissions de l'utilisateur? Win11 comment définir les permissions de l'utilisateur
Factory high-precision positioning management system, digital safety production management
How does win11 set user permissions? Win11 method of setting user permissions
Photoshop plug-in HDR (II) - script development PS plug-in
ATSS:自动选择样本,消除Anchor based和Anchor free物体检测方法之间的差别
七夕表白攻略:教你用自己的专业说情话,成功率100%,我只能帮你们到这里了啊~(程序员系列)
[IDM] IDM downloader installation
Trace the source of drugs and tamp the safety dike
【OpenCV 例程200篇】216. 绘制多段线和多边形
MySQL advanced 4
一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图...
