当前位置:网站首页>【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 .
边栏推荐
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
- 周少剑,很少见
- ATSs: automatically select samples to eliminate the difference between anchor based and anchor free object detection methods
- The picgo shortcut is amazing. This person thinks exactly the same as me
- [PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system
- 2023届春招实习-个人面试过程和面经分享
- 基于PHP的轻量企业销售管理系统
- Please, stop painting star! This has nothing to do with patriotism!
- 分享在大疆DJI(深圳总部)工作的日常和福利
- Pocket network supports moonbeam and Moonriver RPC layers
猜你喜欢

新出生的机器狗,打滚1小时后自己掌握走路,吴恩达开山大弟子最新成果

Idea start command line is too long problem handling

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

Automatic, intelligent and visual! Deeply convinced of the eight designs behind sslo scheme

Summer Challenge harmonyos canvas realize clock
![[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

Some abilities can't be learned from work. Look at this article, more than 90% of peers

Malaysia's Star: Sun Yuchen is still adhering to the dream of digital economy in WTO MC12

2022 Moonriver全球黑客松优胜项目名单

Pico,能否拯救消费级VR?
随机推荐
Im instant messaging develops a message delivery scheme for 10000 people
TensorFlow團隊:我們沒被拋弃
七夕表白攻略:教你用自己的专业说情话,成功率100%,我只能帮你们到这里了啊~(程序员系列)
【LeetCode】43. String multiplication
One revolution, two forces, three links: the "carbon reduction" roadmap behind the industrial energy efficiency improvement action plan
ABAP call restful API
使用腾讯云搭建图床服务
How to adjust the size of computer photos to what you want
Pico,能否拯救消费级VR?
Automatique, intelligent, visuel! Forte conviction des huit conceptions derrière la solution sslo
Malaysia's Star: Sun Yuchen is still adhering to the dream of digital economy in WTO MC12
[open source data] open source data set for cross modal (MRI, Meg, eye movement) human spatial memory research based on virtual reality scenes
Tensorflow team: we haven't been abandoned
What is the forkjoin framework in the concurrent programming series?
#夏日挑战赛# HarmonyOS canvas实现时钟
Sales management system of lightweight enterprises based on PHP
Redis seckill demo
Win11如何设置用户权限?Win11设置用户权限的方法
自动、智能、可视!深信服SSLO方案背后的八大设计
Tanabata confession introduction: teach you to use your own profession to say love words, the success rate is 100%, I can only help you here ~ (programmer Series)
