当前位置:网站首页>Valid parentheses ---2022/02/23
Valid parentheses ---2022/02/23
2022-06-11 17:34:00 【City Pig】
Valid parenthesis
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 .
Title source
Their thinking
Ideas :
Using stack knowledge , Last in, first out
If it's left parenthesis , Pressure into the stack ;
If it's right bracket , Determine whether the top left bracket matches , If it matches, it will be directly out of the stack , Otherwise return to false; After character traversal , And there are no non-existent elements in the stack , Then return to true. The thinking here is , How to construct a stack , And use the corresponding operations to achieve the stack and stackHarvest :
1、J ava Stack class in : Introduction package Stack
2、 With the stack Stack Create objects ( Different types )
Stack stack = new Stack<>();
Stack stack = new Stack<>();
3、 Consider two special cases , If the left parenthesis is entered first and no match is found : At this point, judge
After all the closing parentheses match , Whether there are still elements in the stack ; If the preceding parentheses
And complete the matching , How does the last closing parenthesis find unmatched information , At this point, it is necessary to check
Check whether there are any elements in the stack at this time .
4、 If the input is empty , expand : The stack class is used in the case of first in and last out , For example, the judgment of brackets , Expression solving, etc .
Code implementation
import java.util.Stack;
public class IsValid_0223 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="{]";
System.out.println(isValid(str));
}
public static boolean isValid(String s) {
Stack<Character> st=new Stack<>();
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='{'|s.charAt(i)=='['|s.charAt(i)=='(') {
st.push(s.charAt(i));
}
else {
if(st.empty())
{
return false;
}
char te=st.pop();
switch(s.charAt(i)) {
case '}':
if(te=='{') {
break;}
else{
return false;}
case ']':
if(te=='[') {
break;}
else{
return false;}
case ')':
if(te=='(') {
break;}
else{
return false;}
}
}
}
if(st.isEmpty()) return true;
return false;
}
}
Performance evaluation and analysis

The performance of methods submitted on the platform is not high , This is because calling system functions is more time-consuming than implementing functions written by yourself . However, you should be familiar with how to call library functions .
边栏推荐
- Activity | authing's first channel cooperation activity came to a successful conclusion
- 搜狐全员遭诈骗,暴露哪些问题?
- Kubernetes deploys elk and collects container logs using filebeat
- 信息安全数学基础 Chapter 3——有限域(一)
- 小白在同花顺上直接办理账户是安全的吗?
- 04_特征工程—特征选择
- TypeScipt基础
- Typescript learning notes (II)
- [MySQL] detailed explanation of redo log, undo log and binlog (4)
- Don't you understand the design and principle of thread pool? Break it up and crush it. I'll teach you how to design the thread pool
猜你喜欢

合并两个有序链表---2022/02/24

Leetcode force deduction question

测试基础之:黑盒测试

Docker installs mysql5.7 (enable binlog function and modify characters)

【Mysql】redo log,undo log 和binlog详解(四)

Guide to Dama data management knowledge system: percentage of chapter scores

【线上问题】Timeout waiting for connection from pool 问题排查

Vscode automatic eslint formatting when saving code

Format eslint automatique lorsque vscode enregistre le Code

vscode保存代碼時自動eslint格式化
随机推荐
搜狐全員遭詐騙,暴露哪些問題?
Qlineedit set input mask
Hash表、 继承
tidb-写热点的测试及分析
Sohu tout le personnel a été escroqué, quels problèmes ont été exposés?
[MySQL] detailed explanation of redo log, undo log and binlog (4)
av_read_frame返回值为-5 Input/output error
有效的括号---2022/02/23
信息安全数学基础 Chapter 4——二次剩余与方根
通过Xshell连接有跳板机/堡垒机的服务器
vscode配置eslint自动格式化报错“The setting is deprecated. Use editor.codeActionsOnSave instead with a source“
7-1 均是素数
tidb-cdc创建任务报错 Unknown or incorrect time zone
6-3 读文章(*)
Authing Share|理解 SAML2 协议
which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mod
Leetcode force deduction question
6-6 批量求和(*)
Go get downloaded package path
The use of histogram function in MATLAB