当前位置:网站首页>[record of question brushing] 20. Valid brackets
[record of question brushing] 20. Valid brackets
2022-07-24 18:14:00 【InfoQ】
One 、 Title Description
- Opening parentheses must be closed with closing parentheses of the same type .
- The left parenthesis must be closed in the correct order .
Input :s = "()"
Output :true
Input :s = "()[]{}"
Output :true
Input :s = "(]"
Output :false
Input :s = "([)]"
Output :false
Input :s = "{[]}"
Output :true
- 1 <= s.length <= 104
- s Brackets only '()[]{}' form
II. Train of thought analysis
Stack to solve the problem First in, then out - When encountering the left parenthesis , Push
- When you encounter the right parenthesis , Take out the top bracket
- Judge out brackets Whether it is consistent with the type of right parenthesis , atypism , Go straight back to
false
- If there are no left parentheses in the stack , And go straight back to
false
- When all strings are traversed
- The stack is empty. , It means that all parentheses are closed
- Stack is not empty. , Note that there are parentheses that do not match the closure
- When it comes to matching , The string length must be even , Otherwise, it will not match , Go straight back to
false
- For easy matching , We can use a hash table to store every kind of parenthesis . The key is a right parenthesis , Values are left parentheses of the same type .
3、 ... and 、 Code implementation
class Solution {
public boolean isValid(String s) {
int length = s.length();
if (length % 2 != 0) {
return false;
}
Map<Character, Character> map = new HashMap<Character, Character>() {{
put(')', '(');
put(']', '[');
put('}', '{');
}};
Deque<Character> stack = new LinkedList<Character>();
for (int i = 0; i < length; i++) {
char ch = s.charAt(i);
if (map.containsKey(ch)) {
if (stack.isEmpty() || stack.peek() != map.get(ch)) {
return false;
}
stack.pop();
} else {
stack.push(ch);
}
}
return stack.isEmpty();
}
}
Complexity analysis
nsRunning results

summary
边栏推荐
- SSM framework learning
- How does win11 enhance the microphone? Win11 enhanced microphone settings
- odoo中的bom理解
- [opencv] - thresholding
- 关于接口的写法 1链式判读 ?. 2方法执行 (finally)一定会执行
- com.mysql.cj.jdbc.exceptions. MySQLTransactionRollbackException: Deadlock found when trying to get lo
- Definition and storage of adjacency table and adjacency storage of directed graph and undirected graph
- 【obs】视频、音频编码与rtmp发送的配合
- Bib | mol2context vec: context aware deep network model learning molecular representation for drug discovery
- Stream, file, IO
猜你喜欢

redis集群的三种方式

【obs】依赖库: x264 vs 构建

手写博客平台~第二天

After separation, the impression notes are still difficult to live, but there are many coquettish operations

安装JumpServer

JMeter -- silent operation

继承与派生

Shanghai Jiaotong University team used joint deep learning to optimize metabonomics research

How does win11 enhance the microphone? Win11 enhanced microphone settings

PXE efficient batch network installation
随机推荐
About the writing method of interface 1 chain interpretation 2. Method execution (finally) must be executed
Single cell code analysis - gynecological cancer single cell transcriptome and chromatin accessibility analysis 1
Array object methods commonly used traversal methods & higher-order functions
Section 10 cache breakdown follow Daewoo redis ------- directory post
Install jumpserver
Problems needing attention in writing pages
Still reading logs on the command line? Use kibana, visual log analysis yyds!
XSS绕过姿势总结
Ship new idea 2022.2 was officially released, and the new features are really fragrant!
(mandatory) override equals must override hashcode (principle analysis)
Common methods of string (2)
SSM framework learning
Go language interface and type
mac数据库管理软件Navicat Premium Essentials Mac
Definition and storage of adjacency table and adjacency storage of directed graph and undirected graph
PXE efficient batch network installation
[leetcode] 30. Concatenate substrings of all words
Alibaba /1688 API instructions for searching products by map (pailitao)
Laravel笔记-用户登录时密码进行RSA加密(提高系统安全性)
Baidu touch.js