当前位置:网站首页>【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 .
边栏推荐
猜你喜欢

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)

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

圈铁发音,动感与无噪强强出彩,魔浪HIFIair蓝牙耳机测评

TensorFlow团队:我们没被抛弃

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

【Hot100】19. 删除链表的倒数第 N 个结点
![[daily news]what happened to the corresponding author of latex](/img/0f/d19b27dc42124c89993dee1bada838.png)
[daily news]what happened to the corresponding author of latex

ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification

求求你们,别再刷 Star 了!这跟“爱国”没关系!

ATSs: automatically select samples to eliminate the difference between anchor based and anchor free object detection methods
随机推荐
u本位合约和币本位合约有区别,u本位合约会爆仓吗
Action after deleting laravel's model
How to write good code - Defensive Programming Guide
Pico,是要拯救还是带偏消费级VR?
Pico, can we save consumer VR?
Nuxt.js数据预取
Seate中用了shardingjdbc 就不能用全局事务了吗?
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
Can't global transactions be used when shardingjdbc is used in seate?
【Hot100】20. 有效的括号
软件测试的可持续发展,必须要学会敲代码?
process.env.NODE_ENV
idea启动Command line is too long问题处理
[IDM] IDM downloader installation
Équipe tensflow: Nous ne sommes pas abandonnés
Huawei issued hcsp-solution-5g security talent certification to help build 5g security talent ecosystem
HR面试:最常见的面试问题和技巧性答复
The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's eldest disciple
近半年内连获5家“巨头”投资,这家智能驾驶“黑马”受资本追捧
开机时小键盘灯不亮的解决方案
