当前位置:网站首页>【Hot100】20. Valid parentheses
【Hot100】20. Valid parentheses
2022-07-01 16:05:00 【Wang Liuliu's it daily】
- Use 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;
}
- Don't use map, With the stack
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();
}
}
- Double queue
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);
// Touching the left bracket , Put the corresponding right parenthesis on the stack
if (ch == '(') {
deque.push(')');
}else if (ch == '{') {
deque.push('}');
}else if (ch == '[') {
deque.push(']');
} else if (deque.isEmpty() || deque.peek() != ch) {
return false;
}else {
// If it is a right parenthesis, judge whether it matches the top element of the stack
deque.pop();
}
}
// Finally, determine whether the elements in the stack match
return deque.isEmpty();
}
}
In the iteration process , Find inconsistent brackets in advance and return , Improve the efficiency of the algorithm .
边栏推荐
- picgo快捷键 绝了这人和我的想法 一模一样
- 实现数字永生还有多久?元宇宙全息真人分身#8i
- In the era of super video, what kind of technology will become the base?
- 硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件
- 有些能力,是工作中学不来的,看看这篇超过90%同行
- Nuxt. JS data prefetching
- 分享在大疆DJI(深圳总部)工作的日常和福利
- Équipe tensflow: Nous ne sommes pas abandonnés
- Introduction to RT thread env tool (learning notes)
- 工厂高精度定位管理系统,数字化安全生产管理
猜你喜欢

ATSS:自动选择样本,消除Anchor based和Anchor free物体检测方法之间的差别

Hardware development notes (9): basic process of hardware development, making a USB to RS232 module (8): create asm1117-3.3v package library and associate principle graphic devices

【OpenCV 例程200篇】216. 绘制多段线和多边形

Introduction to RT thread env tool (learning notes)

Zhou Shaojian, rare

Automatic, intelligent and visual! Deeply convinced of the eight designs behind sslo scheme

Comment win11 définit - il les permissions de l'utilisateur? Win11 comment définir les permissions de l'utilisateur

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

Smart Party Building: faith through time and space | 7.1 dedication

For the sustainable development of software testing, we must learn to knock code?
随机推荐
One revolution, two forces, three links: the "carbon reduction" roadmap behind the industrial energy efficiency improvement action plan
Tensorflow team: we haven't been abandoned
In the era of super video, what kind of technology will become the base?
laravel的模型删除后动作
Problèmes rencontrés dans le développement de la GI pour maintenir le rythme cardiaque en vie
【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)
[daily news]what happened to the corresponding author of latex
Malaysia's Star: Sun Yuchen is still adhering to the dream of digital economy in WTO MC12
Where should older test / development programmers go? Will it be abandoned by the times?
There is a difference between u-standard contract and currency standard contract. Will u-standard contract explode
开机时小键盘灯不亮的解决方案
[PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system
Pico,能否拯救消费级VR?
Does 1.5.1 in Seata support mysql8?
Action after deleting laravel's model
七夕表白攻略:教你用自己的专业说情话,成功率100%,我只能帮你们到这里了啊~(程序员系列)
Pico, can we save consumer VR?
idea启动Command line is too long问题处理
从 MLPerf 谈起:如何引领 AI 加速器的下一波浪潮
Talking from mlperf: how to lead the next wave of AI accelerator
