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

边栏推荐
- [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
- Map operation execution process
- The 19th Zhejiang Provincial College Programming Contest 2022 f.easyfix chairman tree
- LM12丨Rolling Heikin Ashi二重K线滤波器
- Oracle database backup and recovery
- Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
- 2022 Season 6 perfect children's model Shaanxi finals came to a successful conclusion
- Pycharm essential plug-in, change the background (self use, continuous update) | CSDN creation punch in
- C number of words, plus ¥, longest word, average value
- Slam interview summary
猜你喜欢

Right click the idea file to create new. There is no solution to create new servlet

Open source hardware small project: anxinco esp-c3f control ws2812
![给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」](/img/21/2e99dd6173ab4925ec22290cd4a357.png)
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」

0-1 knapsack problem

建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置

Explain

Pycharm essential plug-in, change the background (self use, continuous update) | CSDN creation punch in

Ora-02437 failed to verify the primary key violation

Map operation execution process
![Given an array, such as [7864, 284, 347, 7732, 8498], now you need to splice the numbers in the array to return the](/img/21/2e99dd6173ab4925ec22290cd4a357.png)
Given an array, such as [7864, 284, 347, 7732, 8498], now you need to splice the numbers in the array to return the "largest possible number."
随机推荐
系统设计概述
The 19th Zhejiang Provincial College Programming Contest VP record + supplementary questions
做自媒体视频剪辑怎么赚钱呢?
postgis学习
Anxin can internally test offline voice module vb-01 to communicate with esp-c3-12f
HDU 4747 mex "recommended collection"
【7.4】25. Turn over the linked list in groups of K
Dependency injection 2 advantage lifecycle
SAP HR 家庭成员信息
0-1 knapsack problem
Flash encryption process and implementation of esp32
Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
Sequence of entity layer, Dao layer, service layer and controller layer
How to change the formula picture in the paper directly into the formula in word
B_QuRT_User_Guide(40)
Anxinco EC series modules are connected to the multi protocol access products of onenet Internet of things open platform
SAP HR奖罚信息导出
[stm32+esp8266 connect Tencent cloud IOT development platform 2] stm32+esp8266-01s connect Tencent cloud
欢聚时代一面