当前位置:网站首页>【力扣】括号匹配
【力扣】括号匹配
2022-07-17 05:13:00 【Patrick star`】
题目:
给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
思路:
利用栈后进先出的特性

代码:
typedef struct Stack
{
char* c;
int top;
int capacity;
}Stack;
void StackInit(Stack* ps)
{
ps->top = 0;
ps->c = NULL;
ps->capacity = 0;
}
void StackPush(Stack* ps, char c)
{
if (ps->capacity == ps->top)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
char* temp = (char*)realloc(ps->c,sizeof(char) * newCapacity);
if (!temp)
{
exit(-1);
}
ps->c = temp;
ps->capacity = newCapacity;
}
ps->c[ps->top++] = c;
}
void StackPop(Stack* ps)
{
ps->top--;
}
char StackTop(Stack* ps)
{
return ps->c[ps->top - 1];
}
bool isValid(char* s)
{
char c = 0;
Stack stack;
StackInit(&stack);
while (*s)
{
if (*s == '(' || *s == '{' || *s == '[')
{
StackPush(&stack, *s);
}
else
{
if (stack.top == 0)
{
return false;
}
if (*s == ')')
{
c = StackTop(&stack);
if (c != '(')
{
return false;
}
StackPop(&stack);
}
else if (*s == ']')
{
c = StackTop(&stack);
if (c != '[')
{
return false;
}
StackPop(&stack);
}
else if (*s == '}')
{
c = StackTop(&stack);
if (c != '{')
{
return false;
}
StackPop(&stack);
}
}
s++;
}
if (stack.top == 0)
{
return true;
}
return false;
}边栏推荐
- DSL实现Bucket聚合
- Qtss data type
- 4-channel encoder pulse counter, 8-Channel do, Modbus TCP module
- Conversion, isolation and transmission of international standard signals 0-5v/0-10v/1-5v, 0-10ma/0-20ma/4-20ma, etc
- Hm8203 linear two string charging management controller IC
- c语言 指定日期开始多少天 显示
- Minio installation, deployment and simple use
- 2022/07/09 第五小组 丁帅 学习笔记 day02
- Cours de mathématiques de base 2 Fonction Euler, écran linéaire, élargissement de l'Europe
- RestClient-多条件聚合
猜你喜欢

无线充发光鼠标垫RGB LED照明无线充电鼠标垫

Rs-485/232 to 4-20ma/0-10v isolated d/a converter

go语言介绍及应用场景分析

Introduction to goroutine, a high concurrency feature of golang

[antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique.....

IT4058A型号单节锂离子电池充电管理

2021-09-15
![[simple and fast] after startup, the desktop is normal, and the taskbar below is unresponsive / the mouse keeps turning](/img/65/fbb975491d4abd5d1babdf000513e2.png)
[simple and fast] after startup, the desktop is normal, and the taskbar below is unresponsive / the mouse keeps turning

5-17陕西科技大学的隐藏学生服务

Hm9922 switching buck LED constant current driver IC
随机推荐
5-17陕西科技大学的隐藏学生服务
HRA isolation series wide voltage input positive and negative high voltage regulated output
Ehab the Xorcist (异或性质,构造)
Vscode configuring golang development environment
HM agent multi section lithium battery charging IC
计算几何(2)
比例阀放大板1A、2A、3A、5A比例阀驱动模块0-10V转0-24V
Baby Ehab Partitions Again(dp,构造,位运算)
Basic mathematics course 2_ Euler function, linear sieve, extended Euler
minio安装部署及简单使用
Decorate Apple Tree
Darwin Streaming Server 介绍
简单Chrome脚本 自动跳过b站视频播放结束后的的充电鸣谢页面
有依赖的背包,狭义(二进制枚举),广义(树形dp)
Introduction to Darwin streaming server
Lithium battery charging management chip
filezilla传输虚拟机速度慢解决方法
[USACO06DEC]The Fewest Coins G(混合背包)
串口循环缓存区 简单 免初始化 不用堆、指针、分段memcpy
RestAPI实现聚合(黑马教程)