当前位置:网站首页>leetcode刷题:栈与队列03(有效的括号)
leetcode刷题:栈与队列03(有效的括号)
2022-07-01 19:16:00 【涛涛英语学不进去】
20. 有效的括号
给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串,判断字符串是否有效。
有效字符串需满足:
- 左括号必须用相同类型的右括号闭合。
- 左括号必须以正确的顺序闭合。
- 注意空字符串可被认为是有效字符串。
示例 1:
- 输入: “()”
- 输出: true
示例 2:
- 输入: “()[]{}”
- 输出: true
示例 3:
- 输入: “(]”
- 输出: false
示例 4:
- 输入: “([)]”
- 输出: false
示例 5:
- 输入: “{[]}”
- 输出: true
显然用栈思想
package com.programmercarl.stacks_queues;
import java.util.Stack;
/** * @ClassName IsValid * @Descriotion TODO * @Author nitaotao * @Date 2022/6/29 13:41 * @Version 1.0 **/
public class IsValid {
public boolean isValid(String s) {
/** * 显然这题用栈做 */
Stack stack = new Stack();
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
//左符号加入栈中
if (chars[i] == '(' || chars[i] == '[' || chars[i] == '{') {
stack.push(chars[i]);
} else {
//如果直接是右括号开头,返回false
if (stack.size() == 0) {
return false;
}
char item = (char) stack.pop();
switch (item) {
case '(':
if (chars[i] != ')') {
return false;
}
break;
case '[':
if (chars[i] != ']') {
return false;
}
break;
case '{':
if (chars[i] != '}') {
return false;
}
break;
}
}
}
return stack.size() == 0;
}
}
发现这题之前做过,看看当时思想。
class Solution {
public boolean isValid(String s) {
String[] arr = s.split("");
if (arr.length % 2 != 0) {
//如果不是偶数,必有一个不匹配
return false;
}
// ( [ { 为进 ) ] }为出
LinkedList queue = new LinkedList(); //栈结构
// for (int i = 0; i < arr.length; i++) {
// System.out.print(arr[i]);
// }
// System.out.println();
for (int i = 0; i < arr.length; i++) {
if (arr[i].equals("{") || arr[i].equals("[") || arr[i].equals("(")) {
queue.addFirst(arr[i]);
} else {
//如果先进来的是右括号
if (queue.size() == 0) {
return false;
}
String left = (String) queue.remove();
if (left.equals("{")) {
if (arr[i].equals("}")) {
continue;
} else {
return false;
}
} else if (left.equals("[")) {
if (arr[i].equals("]")) {
continue;
} else {
return false;
}
} else if (left.equals("(")) {
if (arr[i].equals(")")) {
continue;
} else {
return false;
}
}
}
}
if (queue.size() != 0) {
return false;
}
return true;
}
}
hhhhhh,当时暴力思想,还是太年轻哦。
边栏推荐
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
- Realize pyramids through JS (asterisk pyramid, palindrome symmetric digital pyramid)
- Use of common built-in classes of JS
- 薛定谔的日语学习小程序源码
- Past and present life of product modular design
- 如果浏览器被意外关闭,react怎么缓存用户填写的表单?
- 关于一个神奇函数的用法
- Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
- 深度学习 神经网络基础
- 利用QEventLoop实现同步等待槽函数返回
猜你喜欢
深度学习 常见的损失函数
[multithreading] lock strategy
2022熔化焊接与热切割上岗证题目模拟考试平台操作
[Mysql]安装Mysql5.7
Optimization of the problem that the request flow fails to terminate during page switching of easycvr cluster video Plaza
On the next generation entrance of the metauniverse -- the implementation of brain computer interface
Entering Ruxin Town, digital intelligence transformation connects "future community"
Uniapp uses Tencent map to select points without window monitoring to return users' location information. How to deal with it
联想电脑怎么连接蓝牙耳机?
NSI脚本的测试
随机推荐
关于new Set( )还有哪些是你不知道的
On the next generation entrance of the metauniverse -- the implementation of brain computer interface
EURA欧瑞E1000系列变频器使用PID实现恒压供水功能的相关参数设置及接线
Test of NSI script
关联线探究,如何连接流程图的两个节点
走进如心小镇,数智化变革连接“未来社区”
写博客文档
朋友圈社区程序源码分享
RichView TRVDocParameters 页面参数设置
【C语言】详解 memset() 函数用法
Iframe 父子页面通信
个人炒股怎样开户?安全吗。
NSI脚本的测试
收藏:存储知识全面总结
关于一个神奇函数的用法
随机头像大全,多分类带历史记录微信小程序源码_支持流量主
实战项目笔记(一)——虚拟机的创建
合成大西瓜小游戏微信小程序源码/微信游戏小程序源码
独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
在技术升级中迎合消费者需求,安吉尔净水器“价值战”的竞争之道