当前位置:网站首页>Stack and queue - 20. Valid parentheses
Stack and queue - 20. Valid parentheses
2022-07-24 13:54:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
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 .
2 Title Example
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
3 Topic tips
1 <= s.length <= 104
s Brackets only ‘()[]{}’ form
4 Ideas
To judge the validity of parentheses, you can use 「 Stack 」 this — Data structure to solve .
We iterate over a given string s. When we encounter an open parenthesis , We will expect that in the subsequent traversal , There is a closing bracket of the same type to close it . Because the left bracket encountered after must be closed first , So we can put the left bracket at the top of the stack . When we encounter a closing bracket , We need to close an open parenthesis of the same type . here , We can take out the left parentheses at the top of the stack and judge whether they are the same type of parentheses . If not of the same type , Or there is no left parenthesis in stack , So the string s Invalid , return False. To quickly determine the type of parentheses , We can use a hash table to store every — Kind bracket . The key of the hash table is the right parenthesis , Values are left parentheses of the same type .
After traversing the knot , If there is no left parenthesis in the stack , Note that we will string s All left parentheses in are closed , return True, Otherwise return to False.
Notice the length of the valid string — Set as even number , So if the length of the string is odd , We can go straight back False, Omit the subsequent traversal judgment process .
5 My answer
Method 1 :
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();
}
}
边栏推荐
- R语言epiDisplay包的kap函数计算Kappa统计量的值(总一致性、期望一致性)、对多个评分对象的结果进行一致性分析、评分的类别为多个类别、如果评分中包含缺失值则标准误及其相关统计量则无法计算
- Flink综合案例(九)
- Network security - file upload blacklist bypass
- 如何在Ubuntu 18.04和Debian 9上安装PHP 5.6
- 如何在树莓派上搭建运行 WordPress
- Uni app background audio will not be played after the screen is turned off or returned to the desktop
- 在LNMP架构中搭建Zabbix监控服务
- 网络安全——使用Exchange SSRF 漏洞结合NTLM中继进行渗透测试
- Flink fault tolerance mechanism (V)
- R语言tidyr包的gather函数将从宽表转化为长表(宽表转化为长表)、第一个参数指定原多个数据列名称生成的新数据列名称、第二个参数指定原表内容值、第三个和第四个参数通过列索引指定不变的列名称列表
猜你喜欢

Network security -- Service Vulnerability scanning and utilization

Network security - Web penetration testing

Network security - file upload blacklist bypass

Chapter VI bus

Rhcsa sixth note

在EXCEL表格中如何进行快速换行

网络安全——报错注入

【C语言笔记分享】——动态内存管理malloc、free、calloc、realloc、柔性数组

Csp2021 T3 palindrome

OWASP zap security testing tool tutorial (Advanced)
随机推荐
2022.7.22 模拟赛
如何在树莓派上搭建运行 WordPress
Data modification and insertion
OWASP zap security testing tool tutorial (Advanced)
The R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graphs, uses the by parameter to specify the groupi
Flink advanced features and new features (VIII)
网络安全——文件上传白名单绕过
网络安全——Cookie注入
栈与队列——225. 用队列实现栈
Flinktable & SQL (VII)
Why are there "two abstract methods" in the functional interface comparator?
字符串——459. 重复的子字符串
uni-app 背景音频 熄屏或者退回桌面之后不在播放
Ansible服务常用命令模块详细解析
网络安全——文件上传内容检查绕过
Browser type judgment
Is it safe for Huatai Securities to open an account through channels? Is it formal
Packet switching and label switching in MPLS
Network security - file upload competitive conditions bypass
Nmap security testing tool tutorial