当前位置:网站首页>【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();
}
}
在迭代过程中,提前发现不符合的括号并且返回,提升算法效率。
边栏推荐
- process. env. NODE_ ENV
- Zhang Chi's class: several types and differences of Six Sigma data
- 有些能力,是工作中学不来的,看看这篇超过90%同行
- There will be a gap bug when the search box and button are zoomed
- 大龄测试/开发程序员该何去何从?是否会被时代抛弃?
- MySQL backup and restore single database and single table
- ThinkPHP advanced
- [target tracking] |stark
- C#/VB. Net merge PDF document
- 自動、智能、可視!深信服SSLO方案背後的八大設計
猜你喜欢

Crypto Daily: Sun Yuchen proposed to solve global problems with digital technology on MC12

【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)

How to adjust the color of the computer screen and how to change the color of the computer screen
Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all

Nuxt.js数据预取

Pico,是要拯救还是带偏消费级VR?
![[target tracking] | template update time context information (updatenet)](/img/53/0a8b2135fa4903f30e4573256c393a.png)
[target tracking] | template update time context information (updatenet) "learning the model update for Siamese trackers"

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

【开源数据】基于虚拟现实场景的跨模态(磁共振、脑磁图、眼动)人类空间记忆研究开源数据集

Photoshop插件-HDR(二)-脚本开发-PS插件
随机推荐
process.env.NODE_ENV
2022-07-01日报:谷歌新研究:Minerva,用语言模型解决定量推理问题
[PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system
2022 Moonriver global hacker song winning project list
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
Seata中1.5.1 是否支持mysql8?
Stm32f4-tft-spi timing logic analyzer commissioning record
[open source data] open source data set for cross modal (MRI, Meg, eye movement) human spatial memory research based on virtual reality scenes
跨平台应用开发进阶(二十四) :uni-app实现文件下载并保存
【显存优化】深度学习显存优化方法
Nuxt.js数据预取
[target tracking] | template update time context information (updatenet) "learning the model update for Siamese trackers"
Programming examples of stm32f1 and stm32subeide - production melody of PWM driven buzzer
Smart Party Building: faith through time and space | 7.1 dedication
Summer Challenge harmonyos canvas realize clock
一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图...
【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)
vim 从嫌弃到依赖(22)——自动补全
你TM到底几点下班?!!!
药品溯源夯实安全大堤
