当前位置:网站首页>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,当时暴力思想,还是太年轻哦。
边栏推荐
- How to connect the two nodes of the flow chart
- 如何用OpenMesh创建一个四棱锥
- Customize the insertion of page labels and realize the initial search of similar address books
- 利用QEventLoop实现同步等待槽函数返回
- Win11 how to hide the taskbar? Win11 method to hide the taskbar
- Internship: gradually moving towards project development
- 【Leetcode】最大连续1的个数
- 2022/6/8-2022/6/12
- Architect graduation summary
- How to turn off the boot auto start software in win11
猜你喜欢
tensorflow 张量做卷积,输入量与卷积核维度的理解
Uniapp uses Tencent map to select points without window monitoring to return users' location information. How to deal with it
[mysql] install mysql5.7
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
实战项目笔记(一)——虚拟机的创建
math_ Use differentiation to calculate approximate value
Use Zadig to build a continuous delivery platform from 0 to 1
强大的万年历微信小程序源码-支持多做流量主模式
RichView 文档中的 ITEM
300题线性代数 第四讲 线性方程组
随机推荐
Swiftui 4 new features complete toggle and mixed toggle multiple binding components
How to turn off the boot auto start software in win11
A quietly rising domestic software, low-key and powerful!
How can I know if I want to get the preferential link of stock account opening? Is it safe to open an account online?
internship:复杂json格式数据编写接口
Richview trvdocparameters page parameter settings
NSI脚本的测试
2022年低压电工考试试题及答案
What else do you not know about new set()
【多线程】 实现单例模式 ( 饿汉、懒汉 ) 实现线程安全的单例模式 (双重效验锁)
deb文件安装
【C语言】详解 memset() 函数用法
2022安全员-A证考题及在线模拟考试
Learn white box test case design from simple to deep
随机头像大全,多分类带历史记录微信小程序源码_支持流量主
《軟件工程導論(第六版)》 張海藩 複習筆記
8K HDR!| Hevc hard solution for chromium - principle / Measurement Guide
Error in installing sharp
关于new Set( )还有哪些是你不知道的
深度学习 神经网络基础