当前位置:网站首页>【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 .
边栏推荐
- ATSs: automatically select samples to eliminate the difference between anchor based and anchor free object detection methods
- Use Tencent cloud to build a map bed service
- Uncover the "intelligence tax" of mousse: spend 4billion on marketing, and only 7 invention patents
- 最新NLP赛事实践总结!
- Nuxt.js数据预取
- vscode 查找 替换 一个文件夹下所有文件的数据
- How to write good code - Defensive Programming Guide
- 韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
- Sales management system of lightweight enterprises based on PHP
- The Department came to a Post-00 test paper king who took out 25K. The veteran said it was really dry, but it had been
猜你喜欢
DO280管理应用部署--pod调度控制
有些能力,是工作中学不来的,看看这篇超过90%同行
How to adjust the color of the computer screen and how to change the color of the computer screen
Nuxt.js数据预取
[pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)
[PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system
How does win11 set user permissions? Win11 method of setting user permissions
The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's eldest disciple
vscode 查找 替换 一个文件夹下所有文件的数据
实现数字永生还有多久?元宇宙全息真人分身#8i
随机推荐
One revolution, two forces, three links: the "carbon reduction" roadmap behind the industrial energy efficiency improvement action plan
IM即時通訊開發實現心跳保活遇到的問題
Crypto Daily: Sun Yuchen proposed to solve global problems with digital technology on MC12
C#/VB. Net merge PDF document
Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
Preorder, inorder, follow-up of binary tree (non recursive version)
【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)
【LeetCode】43. 字符串相乘
[daily news]what happened to the corresponding author of latex
Zero copy technology of MySQL
Seata中1.5.1 是否支持mysql8?
Five years after graduation, I became a test development engineer with an annual salary of 30w+
Win11如何设置用户权限?Win11设置用户权限的方法
2022 Moonriver全球黑客松优胜项目名单
高端程序员上班摸鱼指南
ADS算力芯片的多模型架构研究
【OpenCV 例程200篇】216. 绘制多段线和多边形
Detailed explanation of stm32adc analog / digital conversion
Some abilities can't be learned from work. Look at this article, more than 90% of peers
求求你们,别再刷 Star 了!这跟“爱国”没关系!