当前位置:网站首页>【LeetCode】20、有效的括号
【LeetCode】20、有效的括号
2022-07-07 21:52:00 【小曲同学呀】
20、有效的括号
题目:
给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
示例 1:
输入:s = "()"
输出:true
示例 2:
输入:s = "()[]{}"
输出:true
示例 3:
输入:s = "(]"
输出:false
示例 4:
输入:s = "([)]"
输出:false
示例 5:
输入:s = "{[]}"
输出:true
提示:
1 <= s.length <= 10^4
s 仅由括号 ‘()[]{}’ 组成
解题思路:
- 首先如果字符串能组成有效的括号,则长度一定是偶数
- 我们可以遍历字符串,遇到左括号则暂存,期待后面有右括号可以和它匹配
- 如果遇到右括号则检查是否能和最晚暂存的做括号匹配。
- 这就和栈这种数据结构先进后出的特性相吻合了。所以我们可以准备一个栈存放括号对
- 遍历字符串的时候,如果遇到左括号入栈,遇到右括号则判断右括号是否能和栈顶元素匹配,在循环结束的时候还要判断栈是否为空,如果不为空,则不是有效括号匹配的字符串
复杂度分析:
时间复杂度:O(n),其中 n 是字符串 s 的长度。
空间复杂度:O(n+∣Σ∣),其中 Σ 表示字符集,本题中字符串只包含 6 种括号,∣Σ∣=6。栈中的字符数量为 O(n),而哈希表使用的空间为 O(∣Σ∣),相加即可得到总空间复杂度。
具体代码:
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();
}
}

边栏推荐
- USB (XVI) 2022-04-28
- Force deduction solution summary 648 word replacement
- Unity3d Learning Notes 6 - GPU instantiation (1)
- C method question 2
- 高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
- [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
- Explain
- 平衡二叉树【AVL树】——插入、删除
- Lm12 rolling heikin Ashi double K-line filter
- Summary of SQL single table query 2020.7.27
猜你喜欢

PCB wiring rules of PCI Express interface

The efficient s2b2c e-commerce system helps electronic material enterprises improve their adaptability in this way

2021icpc Shanghai h.life is a game Kruskal reconstruction tree

Class C design questions

Live server usage

Markdown

平衡二叉树【AVL树】——插入、删除

SAP HR奖罚信息导出

Lm12 rolling heikin Ashi double K-line filter

Deep understanding of MySQL lock and transaction isolation level
随机推荐
POJ2392 SpaceElevator [DP]
B_QuRT_User_Guide(40)
The for loop realizes 1-100 addition and eliminates the 4-digit tail number
The file format and extension of XLS do not match
B_QuRT_User_Guide(39)
B_ QuRT_ User_ Guide(38)
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
B_QuRT_User_Guide(36)
Extended tree (I) - graphic analysis and C language implementation
SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
2022注册测绘师备考开始 还在不知所措?手把手教你怎么考?
MySQL Index Optimization Practice I
Fibonacci number of dynamic programming
IDEA 2021.3. X cracking
[STM32 + esp-12s connect Tencent cloud IOT development platform 1] creation of cloud platform and burning of at firmware
2022 届的应届生都找到工作了吗?做自媒体可以吗?
Class C design questions
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
ASP. Net core middleware request processing pipeline
8.31 Tencent interview