当前位置:网站首页>[leetcode] 20. Valid brackets
[leetcode] 20. Valid brackets
2022-07-07 23:48:00 【Xiaoqu classmate】
20、 Valid parenthesis
subject :
Given one only includes ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ String s , 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 .
Example 1:
Input :s = "()"
Output :true
Example 2:
Input :s = "()[]{}"
Output :true
Example 3:
Input :s = "(]"
Output :false
Example 4:
Input :s = "([)]"
Output :false
Example 5:
Input :s = "{[]}"
Output :true
Tips :
1 <= s.length <= 10^4
s Brackets only ‘()[]{}’ form
Their thinking :
- First, if the string can form valid parentheses , Then the length must be even
- We can iterate through strings , If the left bracket is encountered, it will be temporarily stored , Expect a closing parenthesis to match it
- If a closing bracket is encountered, check whether it matches the latest temporary bracket .
- This is consistent with the first in and last out characteristics of stack data structure . So we can prepare a stack to store bracket pairs
- When traversing a string , If you encounter an open bracket, put it on the stack , If the right bracket is encountered, judge whether the right bracket can match the top element of the stack , At the end of the loop, it is also necessary to determine whether the stack is empty , If it's not empty , Is not a string matching valid parentheses
Complexity analysis :
Time complexity :O(n), among n Is string s The length of .
Spatial complexity :O(n+∣Σ∣), among Σ Represents the character set , The string in this question only contains 6 Kind bracket ,∣Σ∣=6. The number of characters in the stack is O(n), The space used by the hash table is O(∣Σ∣), Add to get the total space complexity .
Specific code :
class Solution {
public boolean isValid(String s) {
int n = s.length();
if (n % 2 == 1) {
return false;
}
Map<Character, Character> pairs = new HashMap<Character, Character>() {
{
put(')', '(');
put(']', '[');
put('}', '{');
}};
Deque<Character> stack = new LinkedList<Character>();
for (int i = 0; i < n; i++) {
char ch = s.charAt(i);
if (pairs.containsKey(ch)) {
if (stack.isEmpty() || stack.peek() != pairs.get(ch)) {
return false;
}
stack.pop();
} else {
stack.push(ch);
}
}
return stack.isEmpty();
}
}
边栏推荐
- Download AWS toolkit pycharm
- HDU - 1260 tickets (linear DP)
- Data analysis series 3 σ Rule / eliminate outliers according to laida criterion
- One of the anti climbing methods
- Class C design questions
- Anxinco EC series modules are connected to the multi protocol access products of onenet Internet of things open platform
- @Configuration注解的详细介绍
- 一个测试工程师的7年感悟 ---- 致在一路独行的你(别放弃)
- Chisel tutorial - 01 Introduction to Scala
- Aitm3.0005 smoke toxicity test
猜你喜欢
MP4文件格式解析之结合实例分析
Rectification characteristics of fast recovery diode
HB 5469 combustion test method for non-metallic materials in civil aircraft cabin
What if once again forgets the login password of raspberry pie? And you don't have a monitor yet! Today, I would like to introduce a method
[experiment sharing] log in to Cisco devices through the console port
SAP HR labor contract information 0016
FFA与ICGA造影
Benchmarking Detection Transfer Learning with Vision Transformers(2021-11)
Anxinco EC series modules are connected to the multi protocol access products of onenet Internet of things open platform
平衡二叉樹【AVL樹】——插入、删除
随机推荐
go time包常用函数
C cat and dog
Wechat applet development beginner 1
Jisuan Ke - t3104
正畸注意事项(持续更新中)
Idea automatically generates serialVersionUID
BSS 7230 航空内饰材料阻燃性能测试
postgis学习
Where are you going
Get started with mongodb
Anti climbing means cracking the second
SAP HR reward and punishment information export
SAP memory parameter tuning process
平衡二叉树【AVL树】——插入、删除
ASP. Net open web page
数据库面试题+解析
Alibaba cloud MySQL cannot connect
Archery installation test
C number of words, plus ¥, longest word, average value
Svn relocation