当前位置:网站首页>Leetcode skimming: stack and queue 03 (valid parentheses)
Leetcode skimming: stack and queue 03 (valid parentheses)
2022-07-02 00:26:00 【Taotao can't learn English】
20. Valid parenthesis
Given one only includes ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ String , Determines whether the string is valid .
Valid string needs to meet :
- Opening parentheses must be closed with closing parentheses of the same type .
- The left parenthesis must be closed in the correct order .
- Note that an empty string can be considered a valid string .
Example 1:
- Input : “()”
- Output : true
Example 2:
- Input : “()[]{}”
- Output : true
Example 3:
- Input : “(]”
- Output : false
Example 4:
- Input : “([)]”
- Output : false
Example 5:
- Input : “{[]}”
- Output : true
Obviously, with the idea of stack
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) {
/** * Obviously, this problem is done by stack */
Stack stack = new Stack();
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
// The left symbol is added to the stack
if (chars[i] == '(' || chars[i] == '[' || chars[i] == '{') {
stack.push(chars[i]);
} else {
// If it starts directly with a right parenthesis , return 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;
}
}
I have done this before , Look at the thought at that time .
class Solution {
public boolean isValid(String s) {
String[] arr = s.split("");
if (arr.length % 2 != 0) {
// If it's not even , There must be a mismatch
return false;
}
// ( [ { To advance ) ] } For out
LinkedList queue = new LinkedList(); // The stack structure
// 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 the first one is the right bracket
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, At that time, violent thoughts , Still too young .
边栏推荐
- 【CMake】Qt creator 里面的 cmake 配置
- 下载在线视频 m3u8使用教程
- Example explanation: move graph explorer to jupyterlab
- Halcon knowledge: an attempt of 3D reconstruction
- js 公共库 cdn 推荐
- GCC compilation
- LeetCode中等题题分享(5)
- 【opencv】train&test HOG+SVM
- Asp .NetCore 微信订阅号自动回复之文本篇
- Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
猜你喜欢
Export default the exported object cannot be deconstructed, and module Differences between exports
牛客-练习赛101-推理小丑
Comprehensive usage and case questions of sub query of SQL data analysis [patient sorting]
SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
Correlation - intra group correlation coefficient
Windows installation WSL (II)
Pytorch learning record
JS——图片转base码 、base转File对象
如何提升数据质量
Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education
随机推荐
S32Kxxx bootloader之UDS bootloader
SQL Server 安裝指南
LeetCode中等题题分享(5)
Using multithreaded callable to query Oracle Database
Node -- add compressed file
Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging
Operate database transactions with jpatractionmanager
微信小程序缓存过期时间的相关设置(推荐)
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
Ldr6035 smart Bluetooth audio can continuously charge and discharge mobile devices
启牛学院开户安全的吗?开户怎么开?
回顾数据脱敏系统
JS -- image to base code, base to file object
Record the accidental success and failure of uploading large files
[embedded system course design] a single key controls the LED light
下载在线视频 m3u8使用教程
cookie、session、tooken
JS——图片转base码 、base转File对象
Database -- sqlserver details