当前位置:网站首页>有效的括号---2022/02/23
有效的括号---2022/02/23
2022-06-11 17:18:00 【城猪猪】
题目描述
给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
题目来源
解题思路
思路:
利用栈的知识,后进先出来进行判断
如果是左括号,压入栈中;
如果是右括号,判断最顶上的左括号是否匹配,如果匹配直接出栈,否则返回false;字符遍历结束之后,且栈中没有不存在元素,则返回true。这里的思考是,如何来构造栈,并使用相应的操作来实现出栈与入栈收获:
1、J ava中的栈类:引入包Stack
2、用栈Stack 创建对象(类型不同)
Stack stack = new Stack<>();
Stack stack = new Stack<>();
3、考虑两种特殊情况,如果左括号先入找不到匹配的情况:此时要判断
所有的右括号都匹配之后,栈里面是否还存在元素;如果前面的括号
以及完成匹配,后入的右括号如何发现无法匹配信息,此时就需要检
查此时栈中是否有元素存在。
4、如果输入为空的情况, 拓展:栈类对于需要先进后出的情况使用,比如括号的判断,表达式的求解等。
代码实现
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;
}
}
性能评估与分析

平台上提交的方法性能并不高,这是因为调用系统函数会比自己写的函数实现更费时。不过也当熟练一下如何调用库函数吧。
边栏推荐
- Go path: goroot & gopath
- Derivation of child numbering formula for nodes numbered I in full k-ary tree
- Authing biweekly news: online application market (5.10-5.22)
- 拜登下令强制推行零信任架构
- LeetCode-859. 亲密字符串
- Connection and difference of network streaming media protocol (RTP RTCP RTMP HLS)
- Is it safe for Xiaobai to open an account directly on the flush?
- Authing biweekly news: authing forum launched (4.25-5.8)
- Real time myth -- real-time RTOS multitask performance analysis
- LeetCode-859. Intimate string
猜你喜欢

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

Analyze which should be tested in PMP and ACP with actual cases? Which is more useful?
![[MySQL] detailed explanation of redo log, undo log and binlog (4)](/img/67/6e646040c1b941c270b3efff74e94d.png)
[MySQL] detailed explanation of redo log, undo log and binlog (4)

Analysis report on competition pattern and future development strategy of China's corn industry 2022-2028 Edition

Axi protocol Basics

What subclasses inherit, polymorphism, and upward transformation

Hands on deep learning - multiple input and output channels in the convolution layer

子类继承了什么、多态、 向上转型

Solr (I) installation and permission control of Solr

Connection and difference of network streaming media protocol (RTP RTCP RTMP HLS)
随机推荐
Authing CEO 谢扬入选福布斯 2021 年 30 Under 30 亚洲榜单
Talk about the interview questions of the collection
“is-a”,“has-a”,“like-a”
What is the minimum change price of PTA futures? How can PTA futures be safe?
Recyclerview cache reuse analysis, source code interpretation
6-7 文件读写操作
拜登下令强制推行零信任架构
6-6 批量求和(*)
Typescipt Basics
Global and China Mobile Network Optimization (MnO) industry development panoramic survey and Investment Strategy Research Report 2022-2028
Leetcode force deduction question
Meituan won the first place in fewclue in the small sample learning list! Prompt learning+ self training practice
Create database instance
CentOS7服务器配置(四)---安装redis
活动 | Authing 首次渠道合作活动圆满落幕
【Mysql】redo log,undo log 和binlog详解(四)
The use of histogram function in MATLAB
6-5 统计单词数量(文件)(*)
Authing Share|理解 SAML2 协议
Hands on deep learning - multiple input and output channels in the convolution layer