当前位置:网站首页>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,当时暴力思想,还是太年轻哦。
边栏推荐
- Penetration tools - trustedsec's penetration testing framework (PTF)
- Keras机器翻译实战
- internship:逐渐迈向项目开发
- Practical project notes (I) -- creation of virtual machine
- 2022熔化焊接与热切割上岗证题目模拟考试平台操作
- 利用QEventLoop实现同步等待槽函数返回
- Vulnerability recurrence - Net ueeditor upload
- EURA欧瑞E1000系列变频器使用PID实现恒压供水功能的相关参数设置及接线
- 多个张量与多个卷积核做卷积运算的输出结果
- internship:复杂json格式数据编写接口
猜你喜欢

牛客编程题--必刷101之字符串(高效刷题,举一反三)

GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速

math_ Use differentiation to calculate approximate value

Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers

How to create a pyramid with openmesh

RichView TRVDocParameters 页面参数设置

走进如心小镇,数智化变革连接“未来社区”

极客DIY开源方案分享——数字幅频均衡功率放大器设计(实用的嵌入式电子设计作品软硬件综合实践)

深度学习 常见的损失函数

Practical project notes (I) -- creation of virtual machine
随机推荐
2022年低压电工考试试题及答案
Face recognition system opencv face detection
寫博客文檔
#yyds干货盘点#SQL聚合查询方法总结
STC 32-bit 8051 single chip microcomputer development example tutorial three program compilation setting and download
math_利用微分算近似值
Architect graduation summary
Summary of SQL aggregate query method for yyds dry goods inventory
Solve the problem of slow or failed vscode download
Entering Ruxin Town, digital intelligence transformation connects "future community"
Learn white box test case design from simple to deep
【多线程】 实现单例模式 ( 饿汉、懒汉 ) 实现线程安全的单例模式 (双重效验锁)
After adding cocoapods successfully, the header file cannot be imported or an error is reported in not found file
fastDFS入门
如何用OpenMesh创建一个四棱锥
合成大西瓜小游戏微信小程序源码/微信游戏小程序源码
Swiftui 4 new features complete toggle and mixed toggle multiple binding components
Data analysts sound tall? Understand these points before you decide whether to transform
Niuke programming question -- must brush the string of 101 (brush the question efficiently, draw inferences from one instance)
On the next generation entrance of the metauniverse -- the implementation of brain computer interface