当前位置:网站首页>【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();
}
}

边栏推荐
- Oracle statistics by time
- Lm12 rolling heikin Ashi double K-line filter
- 平衡二叉树【AVL树】——插入、删除
- Dependency injection
- B_ QuRT_ User_ Guide(36)
- 2022 届的应届生都找到工作了吗?做自媒体可以吗?
- 2022 certified surveyors are still at a loss when preparing for the exam? Teach you how to take the exam hand in hand?
- Anxinco esp32-a1s development board is adapted to Baidu dueros routine to realize online voice function
- 伸展树(一) - 图文解析与C语言实现
- C inheritance and interface design polymorphism
猜你喜欢

Anxin can internally test offline voice module vb-01 to communicate with esp-c3-12f

Oracle database backup and recovery

Flash encryption process and implementation of esp32

One week learning summary of STL Standard Template Library

List. How to achieve ascending and descending sort() 2020.8.6

B_ QuRT_ User_ Guide(38)

B_QuRT_User_Guide(36)

MySQL架构

Mobile heterogeneous computing technology - GPU OpenCL programming (basic)

New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
随机推荐
PCB wiring rules of PCI Express interface
sql 数据库执行问题
[untitled]
Lm12 rolling heikin Ashi double K-line filter
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
UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)
Matlab SEIR infectious disease model prediction
How to change the formula picture in the paper directly into the formula in word
B_ QuRT_ User_ Guide(39)
C simple question 2
The for loop realizes 1-100 addition and eliminates the 4-digit tail number
USB (XVIII) 2022-04-17
SAP HR family member information
Progress broadcast | all 29 shield machines of Guangzhou Metro Line 7 have been launched
MongoDB快速入门
Unity3d Learning Notes 6 - GPU instantiation (1)
MySQL架构
欢聚时代一面
8.31 Tencent interview
MySQL Index Optimization Practice II