当前位置:网站首页>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 .
边栏推荐
- 【QT】QtCreator卸载与安装(非正常状态)
- Three methods of finding inverse numbers
- 启牛商学院给的证券账户安不安全?哪里可以开户
- From 20s to 500ms, I used these three methods
- Is it safe to buy funds in a securities account? Where can I buy funds
- Node - generate wechat permission verification configuration
- 基于全志H3的QT5.12.9移植教程
- Practical calculation of the whole process of operational amplifier hysteresis comparator
- B tree and b+tree of MySQL
- 2022 pinduoduo details / pinduoduo product details / pinduoduo SKU details
猜你喜欢
随机推荐
The new version of graphic network PDF will be released soon
Niuke - Practice 101 - reasoning clown
Download the online video m3u8 tutorial
Guide d'installation du serveur SQL
Intelligent operation and maintenance practice: banking business process and single transaction tracking
cookie、session、tooken
export default 导出的对象,不能解构问题,和module.exports的区别
Node - generate wechat permission verification configuration
Leetcode96 different binary search trees
[QT] qtcreator uninstall and installation (abnormal state)
北京炒股开户选择手机办理安全吗?
在证券账户上买基金安全吗?哪里可以买基金
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
Is it safe for qiniu college to open an account? How to open an account?
【mysql 07】GPG key retrieval failed: “Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022“
Iota in golang
Node——Egg 创建本地文件访问接口
I would like to ask, which securities is better for securities account opening? Is it safe to open a mobile account?
Asp .NetCore 微信订阅号自动回复之文本篇
Regular expression collection









