当前位置:网站首页>[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();
}
}
边栏推荐
- 95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
- 企业应用需求导向开发之人力部门,员工考勤记录和实发工资业务程序案例
- Take you hand in hand to build feign with idea
- 35岁那年,我做了一个面临失业的决定
- 正畸注意事项(持续更新中)
- Learn about scratch
- Pycharm essential plug-in, change the background (self use, continuous update) | CSDN creation punch in
- Dependency injection 2 advantage lifecycle
- Chisel tutorial - 01 Introduction to Scala
- c—线性表
猜你喜欢
二叉排序树【BST】——创建、查找、删除、输出
About the difference between ch32 library function and STM32 library function
一键免费翻译300多页的pdf文档
0-1 knapsack problem
Balanced binary tree [AVL tree] - insert, delete
Wechat applet development beginner 1
Anxin can internally test offline voice module vb-01 to communicate with esp-c3-12f
postgis学习
一个测试工程师的7年感悟 ---- 致在一路独行的你(别放弃)
ping报错:未知的名称或服务
随机推荐
Interface
Postgres timestamp to human eye time string or millisecond value
串联二极管,提高耐压
C simple question one
aws-aws help报错
数据湖(十五):Spark与Iceberg整合写操作
Anxin can internally test offline voice module vb-01 to communicate with esp-c3-12f
Anxin vb01 offline voice module access intelligent curtain guidance
MP4文件格式解析之结合实例分析
蓝桥ROS中使用fishros一键安装
Chisel tutorial - 04 Control flow in chisel
一个测试工程师的7年感悟 ---- 致在一路独行的你(别放弃)
Navicat connects Oracle
[stm32+esp8266 connects to Tencent cloud IOT development platform 3] stm32+esp8266-01s dynamically registers devices on Tencent cloud (at instruction mode) -- with source code
ASP. Net open web page
MySQL Architecture
正畸注意事项(持续更新中)
Alibaba cloud MySQL cannot connect
P2141 [noip2014 popularization group] abacus mental arithmetic test
【路径规划】使用垂距限值法与贝塞尔优化A星路径