当前位置:网站首页>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();
}
}
边栏推荐
- Error reported when using activiti to create a database table
- 微信小程序 TODO案例
- 如何在Ubuntu 18.04和Debian 9上安装PHP 5.6
- Remove the treasure box app with the green logo that cannot be deleted from iPhone
- Matlab program for natural gas flow calculation
- The KAP function of epidisplay package in R language calculates the value of kappa statistics (total consistency, expected consistency), analyzes the consistency of the results of multiple scoring obj
- R语言epiDisplay包的kap函数计算Kappa统计量的值(总一致性、期望一致性)、对多个评分对象的结果进行一致性分析、评分的类别为多个类别、如果评分中包含缺失值则标准误及其相关统计量则无法计算
- Network security - error injection
- 通配符(泛域名)SSL证书
- Explain flex layout in detail
猜你喜欢

Network security - file upload penetration test

Matlab program for natural gas flow calculation

Soft link, hard link

Flex layout

Network security - use exchange SSRF vulnerabilities in combination with NTLM trunking for penetration testing

Nessus安全测试工具使用教程

JS execution mechanism

Remove the treasure box app with the green logo that cannot be deleted from iPhone

Sringboot-plugin-framework 实现可插拔插件服务

CSDN garbage has no bottom line!
随机推荐
R语言epiDisplay包的kap函数计算Kappa统计量的值(总一致性、期望一致性)、对多个评分对象的结果进行一致性分析、评分的类别为多个类别、如果评分中包含缺失值则标准误及其相关统计量则无法计算
Flink comprehensive case (IX)
JS get object attribute value
R语言使用sort函数排序向量数据实战、返回实际排序后的数据(默认升序)
在LNMP架构中搭建Zabbix监控服务
栈与队列——20. 有效的括号
Browser type judgment
Lazy loading of pictures
Some simple commands
交换机链路聚合详解【华为eNSP】
Remove the treasure box app with the green logo that cannot be deleted from iPhone
Flink综合案例(九)
数据湖系列文章
rhce第一次作业
在EXCEL表格中如何进行快速换行
The KAP function of epidisplay package in R language calculates the value of kappa statistics (total consistency, expected consistency), analyzes the consistency of the results of multiple scoring obj
Ansible installation and deployment of automated operation and maintenance
网络安全——使用Evil Maid物理访问安全漏洞进行渗透
Editor formula
Error reported when using activiti to create a database table