当前位置:网站首页>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 .
边栏推荐
- It's nothing to be utilitarian!
- 2022 pinduoduo details / pinduoduo product details / pinduoduo SKU details
- 求逆序数的三个方法
- Database -- sqlserver details
- Openvino model performance evaluation tool DL workbench
- 股票开户哪个证券公司比较安全
- LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
- [opencv450] hog+svm and hog+cascade for pedestrian detection
- 【CMake】Qt creator 里面的 cmake 配置
- UDS bootloader of s32kxxx bootloader
猜你喜欢

Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education

【QT】测试Qt是否能连接上数据库

LDR6035智能蓝牙音响可对手机设备持续充放电方案

Windows10 install WSL (I) (wslregisterdistribution error)
![Flow control statement of SQL data analysis [if, case... When detailed]](/img/7b/eabb0700936d34a3a145737580be88.png)
Flow control statement of SQL data analysis [if, case... When detailed]
![Jielizhi, production line assembly link [chapter]](/img/1d/d1736fad33c428e61f450aad512ce0.png)
Jielizhi, production line assembly link [chapter]

数据库--SqlServer详解

RPA tutorial 01: Excel automation from introduction to practice

Export default the exported object cannot be deconstructed, and module Differences between exports

基于全志H3的QT5.12.9移植教程
随机推荐
求逆序数的三个方法
Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education
SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
Example explanation: move graph explorer to jupyterlab
LeetCode 0241. Design priority for arithmetic expressions - DFS
PHP reads ini or env type configuration
SQL数据分析之子查询的综合用法和案例题【耐心整理】
智能运维实战:银行业务流程及单笔交易追踪
Talents come from afar, and Wangcheng district has consolidated the intellectual base of "strengthening the provincial capital"
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
vue 强制清理浏览器缓存
Dongge cashes in and the boss retires?
【模板】自适应辛普森积分
时间复杂度与空间复杂度
Leetcode medium question sharing (5)
[QT] QT cannot find a solution to the compiler using msvc2017
二叉搜索树的创建,查找,添加,删除操作
Flow control statement of SQL data analysis [if, case... When detailed]
实例讲解将Graph Explorer搬上JupyterLab
数据库--SqlServer详解