当前位置:网站首页>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 .
边栏推荐
猜你喜欢
![[embedded system course design] a single key controls the LED light](/img/c9/076618208bbab0b95faa5a7e644a07.png)
[embedded system course design] a single key controls the LED light

It's nothing to be utilitarian!

【QT】Qt 使用MSVC2017找不到编译器的解决办法

Relatively easy to understand PID understanding

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

The origin of usb-if Association and various interfaces

Niuke - Practice 101 - reasoning clown

SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】

【QT】QtCreator卸载与安装(非正常状态)

Dongge cashes in and the boss retires?
随机推荐
EMC circuit protection device for surge and impulse current protection
Material design component - use bottomsheet to show extended content (I)
Selectively inhibiting learning bias for active sampling
Node -- egg creates a local file access interface
ThreadLocal内存泄漏是什么,怎么解决
The new version of graphic network PDF will be released soon
【QT】对于Qt MSVC 2017无法编译的问题解决
Pytorch learning record
Using multithreaded callable to query Oracle Database
vue 强制清理浏览器缓存
Accelerator systems initiative is an independent non-profit organization
【CTF】bjdctf_ 2020_ babystack2
When installing mysql, there are two packages: Perl (data:: dumper) and Perl (JSON)
RPA tutorial 01: Excel automation from introduction to practice
Dongge cashes in and the boss retires?
LeetCode中等题题分享(5)
leetcode96不同的二叉搜索樹
Comprehensive usage and case questions of sub query of SQL data analysis [patient sorting]
UVM tutorial
[opencv450] hog+svm and hog+cascade for pedestrian detection