当前位置:网站首页>[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
边栏推荐
- Alibaba 1688 keyword search product API usage display
- Wu Enda writes: how to establish projects to adapt to AI career
- SSM framework learning
- Goodbye Navicat! This open source database management tool has a cooler interface!
- Still reading logs on the command line? Use kibana, visual log analysis yyds!
- Common methods of array (2)
- 【obs】依赖库: x264 vs 构建
- Go language file operation
- Pycharm configuring opencv Library
- Inherit, override, overload
猜你喜欢

Inherit, override, overload

Install jumpserver

0630~ professional quality course

PXE高效批量网络装机

【obs】依赖库: x264 vs 构建

安装JumpServer

Use of jumpserver

【“码”力全开,“章”显实力】2022年第1季Task挑战赛贡献者榜单

Shengxin commonly used analysis graphics drawing 02 -- unlock the essence of volcano map!

PXE efficient batch network installation
随机推荐
File upload vulnerability -.User.ini and.Htaccess
0621~ES&Lucene
数组扁平化.flat(Infinity)
Common methods of array (2)
Alibaba 1688 keyword search product API usage display
2.3.1 view drawing process
sklearn 的模型保存与加载使用
PXE高效批量网络装机
[network security] analysis vulnerability of website Middleware
Has polardb for PostgreSQL entered the list of Xinchuang database?
如何为超级通胀做好准备
Just one dependency to give swagger a new skin, which is simple and cool!
Inherit, override, overload
Mac database management software Navicat premium essentials mac
Guess JWT keyword
Bib | mol2context vec: context aware deep network model learning molecular representation for drug discovery
Stream, file, IO
0615~用自定义注解实现RBAC权限管理
0630~职业素养课
Section 9 cache penetration follow Daewoo redis ------- directory posts