当前位置:网站首页>【20. 有效的括号】
【20. 有效的括号】
2022-06-22 19:41:00 【爱吃榴莲的喵星人】
一、题目描述
二、做题思路
1.左括号,入栈
2.右括号,左括号出栈跟右括号匹配
三、题目代码
typedef char STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
void StackInit(ST* ps)
{
assert(ps);
ps->a=NULL;
ps->top=0;
ps->capacity=0;
}
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a=NULL;
ps->top=ps->capacity=0;
}
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if(ps->top==ps->capacity)
{
int newcapacity=(ps->capacity==0) ? 4 : 2*ps->capacity;
STDataType*tmp=(STDataType*)realloc(ps->a,sizeof(STDataType)*newcapacity);
if(tmp==NULL)
{
exit(-1);
}
else
{
ps->a=tmp;
ps->capacity=newcapacity;
}
}
ps->a[ps->top]=x;
ps->top++;
}
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top==0;
}
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
STDataType StackTop(ST* ps)
{
assert(ps);
return ps->a[ps->top-1];
}
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
bool isValid(char * s){
ST st;
StackInit(&st);
while(*s)
{
if(*s=='('||*s=='{'||*s=='[')
{
StackPush(&st,*s);
s++;
}
else
{
//遇到右括号了,但是栈里面没有数据,说明
//前面没有左括号,不匹配,返回false s="]"
if(StackEmpty(&st))
{
//[{()}]],这种情况虽然栈是空但入过数据,栈是了开辟空间的
//所以要销毁,防止内存泄露
StackDestroy(&st);
return false;
}
STDataType top=StackTop(&st);
StackPop(&st);
if((*s == ')' && top != '(')
||(*s == '}' && top != '{')
||(*s == ']' && top != '['))
{
//如果s="[[}]",到这里栈里只剩下[
//防止内存泄露要销毁函数
StackDestroy(&st);
return false;
}
s++;
}
}
//如果栈不是空,说明栈中还有左括号未出
//没有匹配。返回是false s="["
bool ret=StackEmpty(&st);
StackDestroy(&st);
return ret;
}
以上是本篇文章的全部内容,如果文章有错误或者有看不懂的地方,多和喵博主交流。互相学习互相进步。如果这篇文章对你有帮助,可以给喵博主一个关注,你们的支持是我最大的动力。
边栏推荐
- [proteus simulation] NE555 delay circuit
- Can financial products be redeemed on weekends?
- EasyClick 固定状态日志窗口
- 91-oracle普通表改分区表的几种方法
- R language usarrests dataset visualization
- Feign常见问题总结
- 【奇葩需求之记录对象不同的日志】
- 一行代码为特定状态绑定SwiftUI视图动画
- Stochastic Adaptive Dynamics of a Simple Market as a Non-Stationary Multi-Armed Bandit Problem
- Three ways of extending ribbon to support Nacos weight
猜你喜欢

Introduction of neural networks for Intelligent Computing (Hopfield network DHNN, CHNN)

R 语言 wine 数据集可视化

让知识付费系统视频支持M3U8格式播放的方法

SwiftUI如何模拟视图发光增大的动画效果

Understand the index of like in MySQL

CVPR 2022 Oral | 视频文本预训练新SOTA,港大、腾讯ARC Lab推出基于多项选择题的借口任务

极客星球 | 业务监控及告警系统体系化建设之路

2022团体程序设计天梯赛L1

采用QTest进行数据集测试-性能测试-GUI测试

Emotion analysis with RNN & CNN pytorch
随机推荐
【剑指Offer】面试题44.数字序列中的某一位数字
已解决:一個錶中可以有多個自增列嗎
[proteus simulation] 8x8LED dot matrix digital cyclic display
Overview of common loss functions for in-depth learning: basic forms, principles and characteristics
R 语言 wine 数据集可视化
MYSQL 几个常用命令使用
采用网络远程访问树莓派。
How to calculate yoy and mom in MySQL
迅睿CMS 自定义数据接口-php执行文件代码
mysql8.0忘记密码的详细解决方法
讲真,Kotlin 协程的挂起没那么神秘(原理篇)
72-最近一次现场生产系统优化的成果与开发建议
程序员必看的学习网站
R 语言nutrient数据集的可视化
How to calculate the Gini coefficient in R (with examples)
A Dynamic Near-Optimal Algorithm for Online Linear Programming
R language usarrests dataset visualization
Kotlin1.6.20 new features context receivers tips
Oh, my God, it's a counter attack by eight part essay
Stochastic Adaptive Dynamics of a Simple Market as a Non-Stationary Multi-Armed Bandit Problem

