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

2022熔化焊接与热切割上岗证题目模拟考试平台操作

Error in installing sharp

联想电脑怎么连接蓝牙耳机?

Arduino Stepper库驱动28BYJ-48步进电机测试程序

新版Free手机、PC、平板、笔记本四端网站缩略展示图在线一键生成网站源码

Penetration tools - trustedsec's penetration testing framework (PTF)

After adding cocoapods successfully, the header file cannot be imported or an error is reported in not found file

运动捕捉系统原理

Principle of motion capture system

目标检测——Yolo系列
随机推荐
Big factories are wolves, small factories are dogs?
强大的万年历微信小程序源码-支持多做流量主模式
ORA-01950
PHP获取微信小程序和小程序商店外链地址
Oracle 死锁测试
寫博客文檔
How can I know if I want to get the preferential link of stock account opening? Is it safe to open an account online?
Review notes of Zhang Haifan in introduction to software engineering (Sixth Edition)
internship:逐渐迈向项目开发
math_利用微分算近似值
极客DIY开源方案分享——数字幅频均衡功率放大器设计(实用的嵌入式电子设计作品软硬件综合实践)
Using qeventloop to realize synchronous waiting for the return of slot function
How to connect the two nodes of the flow chart
实战项目笔记(一)——虚拟机的创建
How to create a pyramid with openmesh
How to turn off the boot auto start software in win11
Set object value changes null value object
Realize pyramids through JS (asterisk pyramid, palindrome symmetric digital pyramid)
想得到股票开户的优惠链接,如何得知?在线开户是安全么?
Test of NSI script