当前位置:网站首页>【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 .
边栏推荐
- ThinkPHP advanced
- 2023届春招实习-个人面试过程和面经分享
- laravel的模型删除后动作
- 跨平台应用开发进阶(二十四) :uni-app实现文件下载并保存
- In the era of super video, what kind of technology will become the base?
- Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
- picgo快捷键 绝了这人和我的想法 一模一样
- 马来西亚《星报》:在WTO MC12 孙宇晨仍在坚持数字经济梦想
- Équipe tensflow: Nous ne sommes pas abandonnés
- Please, stop painting star! This has nothing to do with patriotism!
猜你喜欢

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

Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知

MySQL advanced 4

u本位合约和币本位合约有区别,u本位合约会爆仓吗

2022 Moonriver global hacker song winning project list

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

Problèmes rencontrés dans le développement de la GI pour maintenir le rythme cardiaque en vie

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

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

ADS算力芯片的多模型架构研究
随机推荐
Automatic, intelligent and visual! Deeply convinced of the eight designs behind sslo scheme
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
大龄测试/开发程序员该何去何从?是否会被时代抛弃?
跨平台应用开发进阶(二十四) :uni-app实现文件下载并保存
自动、智能、可视!深信服SSLO方案背后的八大设计
【IDM】IDM下载器安装
Redis seckill demo
高端程序员上班摸鱼指南
I'm a senior test engineer who has been outsourced by Alibaba and now has an annual salary of 40w+. My two-year career changing experience is sad
表格存储中tablestore 目前支持mysql哪些函数呢?
药品溯源夯实安全大堤
Share the daily work and welfare of DJI (Shenzhen headquarters) in Dajiang
新出生的机器狗,打滚1小时后自己掌握走路,吴恩达开山大弟子最新成果
超视频时代,什么样的技术会成为底座?
【LeetCode】43. String multiplication
Sales management system of lightweight enterprises based on PHP
2023 spring recruitment Internship - personal interview process and face-to-face experience sharing
【每日一题】1175. 质数排列
How to adjust the color of the computer screen and how to change the color of the computer screen
2022-07-01日报:谷歌新研究:Minerva,用语言模型解决定量推理问题
