当前位置:网站首页>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,当时暴力思想,还是太年轻哦。
边栏推荐
猜你喜欢

由浅入深学会白盒测试用例设计

STC 32-bit 8051 single chip microcomputer development example tutorial three program compilation setting and download

目標檢測——Yolo系列

Items in richview documents

多个张量与多个卷积核做卷积运算的输出结果

柒微自动发卡系统源码

Simple but modern server dashboard dashdot

Myslq ten kinds of locks, an article will take you to fully analyze

数据分析师听起来很高大上?了解这几点你再决定是否转型

Use of common built-in classes of JS
随机推荐
【let var const】
Practical project notes (I) -- creation of virtual machine
EURA欧瑞E1000系列变频器使用PID实现恒压供水功能的相关参数设置及接线
300 linear algebra Lecture 4 linear equations
2022安全员-A证考题及在线模拟考试
基于图的 Affinity Propagation 聚类计算公式详解和代码示例
强大的万年历微信小程序源码-支持多做流量主模式
合成大西瓜小游戏微信小程序源码/微信游戏小程序源码
NSI脚本的测试
联想电脑怎么连接蓝牙耳机?
Keras机器翻译实战
Problems encountered in installing MySQL in docker Ubuntu container
Review notes of Zhang Haifan in introduction to software engineering (Sixth Edition)
Arduino stepper library drive 28byj-48 stepper motor test program
What else do you not know about new set()
宅男壁纸大全微信小程序源码-带动态壁纸支持多种流量主
寫博客文檔
8K HDR!| Hevc hard solution for chromium - principle / Measurement Guide
How to prevent repeated submission of new orders
Face recognition system opencv face detection