当前位置:网站首页>【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 .
边栏推荐
- 部门来了个拿25k出来的00后测试卷王,老油条表示真干不过,已被...
- Crypto Daily: Sun Yuchen proposed to solve global problems with digital technology on MC12
- DO280管理应用部署--pod调度控制
- When ABAP screen switching, refresh the previous screen
- Introduction to RT thread env tool (learning notes)
- 自動、智能、可視!深信服SSLO方案背後的八大設計
- RT-Thread Env 工具介绍(学习笔记)
- 2023届春招实习-个人面试过程和面经分享
- Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
- [IDM] IDM downloader installation
猜你喜欢

Huawei issued hcsp-solution-5g security talent certification to help build 5g security talent ecosystem

Zhou Shaojian, rare
![[daily news]what happened to the corresponding author of latex](/img/0f/d19b27dc42124c89993dee1bada838.png)
[daily news]what happened to the corresponding author of latex

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

Where should older test / development programmers go? Will it be abandoned by the times?

自動、智能、可視!深信服SSLO方案背後的八大設計

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

AVL 平衡二叉搜索树

Automatique, intelligent, visuel! Forte conviction des huit conceptions derrière la solution sslo

process.env.NODE_ENV
随机推荐
The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's eldest disciple
IM即时通讯开发万人群聊消息投递方案
One revolution, two forces, three links: the "carbon reduction" roadmap behind the industrial energy efficiency improvement action plan
韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
Task. Run(), Task. Factory. Analysis of behavior inconsistency between startnew() and new task()
【Hot100】17. 电话号码的字母组合
Overview | slam of laser and vision fusion
Sales management system of lightweight enterprises based on PHP
Malaysia's Star: Sun Yuchen is still adhering to the dream of digital economy in WTO MC12
开机时小键盘灯不亮的解决方案
使用腾讯云搭建图床服务
跨平台应用开发进阶(二十四) :uni-app实现文件下载并保存
自动、智能、可视!深信服SSLO方案背后的八大设计
Research on multi model architecture of ads computing power chip
AVL balanced binary search tree
Im instant messaging develops a message delivery scheme for 10000 people
July 1, 2022 Daily: Google's new research: Minerva, using language models to solve quantitative reasoning problems
Uncover the "intelligence tax" of mousse: spend 4billion on marketing, and only 7 invention patents
表格存储中tablestore 目前支持mysql哪些函数呢?
毕业后5年,我成为了年薪30w+的测试开发工程师
