当前位置:网站首页>[20. valid brackets]
[20. valid brackets]
2022-06-22 21:06:00 【Cat star people who love Durian】
List of articles
One 、 Title Description
Two 、 How to do it
1. Left parenthesis , Push
2. Right bracket , Left bracket stack matching with right bracket
3、 ... and 、 Title code
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
{
// Encountered a closing bracket , But there is no data in the stack , explain
// There is no left parenthesis in front of it , Mismatch , return false s="]"
if(StackEmpty(&st))
{
//[{()}]], In this case, although the stack is empty, data has been entered , Stack is to open up space
// So destroy , Prevent memory leaks
StackDestroy(&st);
return false;
}
STDataType top=StackTop(&st);
StackPop(&st);
if((*s == ')' && top != '(')
||(*s == '}' && top != '{')
||(*s == ']' && top != '['))
{
// If s="[[}]", All that's left of the stack is [
// To prevent memory leaks, destroy the function
StackDestroy(&st);
return false;
}
s++;
}
}
// If the stack is not empty , It indicates that there are still left parentheses in the stack
// There is no match . Return is false s="["
bool ret=StackEmpty(&st);
StackDestroy(&st);
return ret;
}
The above is the whole content of this article , If there are mistakes in the article or something you don't understand , Communicate more with meow bloggers . Learn from each other and make progress . If this article helps you , Can give meow bloggers a concern , Your support is my biggest motivation .
边栏推荐
- 字节跳动提出轻量级高效新型网络MoCoViT,在分类、检测等CV任务上性能优于GhostNet、MobileNetV3!...
- The road to systematic construction of geek planet business monitoring and alarm system
- R 语言USArrests 数据集可视化
- Software testing - Test Case Design & detailed explanation of test classification
- How to use feign to construct multi parameter requests
- Resolved: can there be multiple auto incrementing columns in a table
- 日本动漫作家和其部分作品
- win10安装.net3.5.docx
- CVPR 2022 oral | video text pre training new SOTA, HKU and Tencent arc lab launched excuse task based on multiple-choice questions
- 采用网络远程访问树莓派。
猜你喜欢
随机推荐
如何使用Feign构造多参数的请求
【文末送书】火遍全网的AI给老照片上色,这里有一份详细教程!
程序员必看的学习网站
【21. 合并两个有序链表】
扩展Ribbon支持Nacos权重的三种方式
Unityeditor editor script execution menu
Several common MySQL commands
【138. 复制带随机指针的链表】
The access succeeds but an exception is thrown: could not find acceptable representation
91-oracle普通表改分区表的几种方法
Kotlin1.6.20 new features context receivers tips
云服务器中安装mysql(2022版)
慕课6、实现负载均衡-Ribbon
R 语言nutrient数据集的可视化
R language Midwest dataset visualization
R语言AirPassengers数据集可视化
89-oracle SQL写法与优化器缺陷一例
How to calculate yoy and mom in MySQL
R语言 co2数据集 可视化
CVPR 2022 oral | video text pre training new SOTA, HKU and Tencent arc lab launched excuse task based on multiple-choice questions









