当前位置:网站首页>【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 .
边栏推荐
- 【Hot100】20. 有效的括号
- 自动、智能、可视!深信服SSLO方案背后的八大设计
- Programming examples of stm32f1 and stm32subeide - production melody of PWM driven buzzer
- How does win11 set user permissions? Win11 method of setting user permissions
- TensorFlow团队:我们没被抛弃
- Win11如何設置用戶權限?Win11設置用戶權限的方法
- Go language learning notes - Gorm use - table addition, deletion, modification and query | web framework gin (VIII)
- One revolution, two forces, three links: the "carbon reduction" roadmap behind the industrial energy efficiency improvement action plan
- HR interview: the most common interview questions and technical answers
- Talking from mlperf: how to lead the next wave of AI accelerator
猜你喜欢

从大湾区“1小时生活圈”看我国智慧交通建设

自动、智能、可视!深信服SSLO方案背后的八大设计

部门来了个拿25k出来的00后测试卷王,老油条表示真干不过,已被...

投稿开奖丨轻量应用服务器征文活动(5月)奖励公布

HR interview: the most common interview questions and technical answers

Detailed explanation of stm32adc analog / digital conversion
![[open source data] open source data set for cross modal (MRI, Meg, eye movement) human spatial memory research based on virtual reality scenes](/img/73/98e4847783be26d86d147425ce3ecd.jpg)
[open source data] open source data set for cross modal (MRI, Meg, eye movement) human spatial memory research based on virtual reality scenes

七夕表白攻略:教你用自己的专业说情话,成功率100%,我只能帮你们到这里了啊~(程序员系列)

德国iF多项大奖加冕,这副耳机有多强?音珀GTW 270 Hybrid深度评测

Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
随机推荐
二叉树的前序,中序,后续(非递归版本)
For the sustainable development of software testing, we must learn to knock code?
IM即时通讯开发万人群聊消息投递方案
Solution to the problem that the keypad light does not light up when starting up
Seate中用了shardingjdbc 就不能用全局事务了吗?
[每日一氵]Latex 的通讯作者怎么搞
Go language learning notes - Gorm use - table addition, deletion, modification and query | web framework gin (VIII)
process. env. NODE_ ENV
Nuxt.js数据预取
Pico, can we save consumer VR?
IM即時通訊開發實現心跳保活遇到的問題
[video memory optimization] deep learning video memory optimization method
Sales management system of lightweight enterprises based on PHP
AVL balanced binary search tree
Samsung took the lead in putting 3nm chips into production, and Shanghai's fresh master students can settle directly. Nankai has established a chip science center. Today, more big news is here
Programming examples of stm32f1 and stm32subeide - production melody of PWM driven buzzer
Microservice tracking SQL (support Gorm query tracking under isto control)
【Hot100】20. 有效的括号
开机时小键盘灯不亮的解决方案
【Hot100】19. 删除链表的倒数第 N 个结点
