当前位置:网站首页>LeetCode每日一练 —— 20. 有效的括号
LeetCode每日一练 —— 20. 有效的括号
2022-08-02 10:31:00 【飞向星的客机】
前言
Wassup guys!我是Edison
今天是 LeetCode 上的 leetcode 20. 有效的括号
Let’s get it!

1. 题目描述
给定一个只包括
'(',')','{','}','[',']'的字符串 s ,判断字符串是否有效。
有效字符串需满足:
1、左括号必须用相同类型的右括号闭合。
2、左括号必须以正确的顺序闭合。
示例 1:
输入:s = "()"
输出:true
示例 2:
输入:s = "()[]{}"
输出:true
示例 3:
输入:s = "(]"
输出:false
示例 4:
输入:s = "([)]"
输出:false
示例 5:
输入:s = "{[]}"
输出:true
2. 思路分析
栈 是一种 先进后出 的数据结构,处理括号问题的时候尤其有用。
思路:
(1)遇到左括号,就入栈
(2)遇到右括号,就去栈中寻找最近的左括号,看是否匹配,然后出栈
步骤:
遍历字符串,遇到前括号直接入栈。
遇到后括号,判断该后括号与栈顶的前括号是否匹配(若此时栈为空,则字符串无效),若不匹配则字符串无效;
若匹配则删除栈顶元素,继续遍历字符串,直到字符串遍历完毕。
当字符串遍历完后,检测栈是否为空,若为空,则字符串有效,若不为空,说明有前括号未匹配,字符串无效。
3. 代码实现
这道题如果用 C 实现的话,就需要我们自己去写一个栈;
而如果用 C++ 实现的话,直接用 STL 即可;
接口代码
typedef char STDataType;
// 支持动态增长的栈
typedef struct Stack
{
STDataType* a;
int top; //栈顶
int capacity; //容量
}ST;
//初始化栈
void StackInit(ST* ps);
//入栈
void StackPush(ST* ps, STDataType x);
//出栈
void StackPop(ST* ps);
//获取栈顶元素
STDataType StackTop(ST* ps);
// 获取栈中有效元素个数
int StackSize(ST* ps);
//检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(ST* ps);
//销毁栈
void StackDestroy(ST* ps);
//————————————————————————————————————————————————————————————————
//初始化栈
void StackInit(ST* ps) {
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
//入栈
void StackPush(ST* ps, STDataType x) {
assert(ps);
//满了扩容
if (ps->top == ps->capacity) {
int newCapacity = ps->capacity == (0) ? (4) : (ps->capacity * 2);
ps->a = realloc(ps->a, newCapacity * sizeof(STDataType));
if (ps->a == NULL) {
printf("realloc fail\n");
exit(-1);
}
ps->capacity = newCapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
//出栈
void StackPop(ST* ps) {
assert(ps);
assert(ps->top > 0);
--ps->top;
}
//获取栈顶元素
STDataType StackTop(ST* ps) {
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
// 获取栈中有效元素个数
int StackSize(ST* ps) {
assert(ps);
return ps->top;
}
//检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(ST* ps) {
assert(ps);
//如果top等于0,就是空
return ps->top == 0;
}
//销毁栈
void StackDestroy(ST* ps) {
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
//————————————————————————————————————————————————————————————————
bool isValid(char * s){
ST st; //先定义一个栈
StackInit(&st);
while (*s)
{
//如果是左括号,就入栈
if (*s == '[' || *s == '(' || *s == '{') {
StackPush(&st, *s); //把左括号放入栈里面
++s;
}
else {
//出栈
/*假设只输入了一个']',那么就会进行出栈判断; 因为输入的是右括号,直接到这里进行判断,那么栈肯定为空,所以直接return false */
if (StackEmpty(&st)) {
return false;
}
char top = StackTop(&st); //把栈顶的左括号拿出来,放到top中
StackPop(&st);
if (*s == ']' && top != '[') {
StackDestroy(&st);
return false;
}
else if (*s == ')' && top != '(') {
StackDestroy(&st);
return false;
}
else if (*s == '}' && top != '{') {
StackDestroy(&st);
return false;
}
else {
//说明匹配了
++s; //匹配,继续比
}
}
}
//栈为空,说明所以左括号都匹配了
//栈不为空,就false,栈为空,就是true
bool ret = StackEmpty(&st);
StackDestroy(&st); //销毁栈
return ret;
}
提交结果
边栏推荐
- 为什么要使用BGP?
- 【术语科普】关于集成工作台那些难懂的词儿,看这篇秒懂!
- MySql tens of millions of paging optimization, fast insertion method of tens of millions of data
- LayaBox---TypeScript---JSX
- Jay Chou's new song is released, crawl the "Mojito" MV barrage, and see what the fans have to say!
- 你认同这个观点吗?大多数企业的数字化都只是为了缓解焦虑
- The heavyweights are coming!Spoilers for the highlights of the Alibaba Cloud Life Science and Intelligent Computing Summit
- The realization of the list
- Three.JS程序化建模入门
- Long battery life or safer?Seal and dark blue SL03 comparison shopping guide
猜你喜欢

How to choose a truly "easy-to-use, high-performance" remote control software

一体化在线政务服务平台,小程序容器技术加速建设步伐

博云入选Gartner中国DevOps代表厂商

链表的实现

3年测试在职,月薪还不足2w,最近被裁员,用亲身经历给大家提个醒...

阿里CTO程立:阿里巴巴开源的历程、理念和实践

云原生应用平台的核心模块有哪些

Turning and anti-climbing attack and defense

You Only Hypothesize Once: 用旋转等变描述子估计变换做点云配准(已开源)

神通数据库,批量插入数据的时候失败
随机推荐
Three.JS程序化建模入门
LayaBox---TypeScript---JSX
games202:三,实时环境光照IBL + PRT
Event 对象,你很了解吗?
R语言ggpubr包的ggline函数可视化分组折线图、add参数为mean_se和dotplot可视化不同水平均值的折线图并为折线图添加误差线(se标准误差)和点阵图、自定义palette设置颜色
R language ggplot2 visualization: use the ggtexttable function of the ggpubr package to visualize tabular data (directly draw tabular graphs or add tabular data to images), use tbody_add_border to add
【术语科普】关于集成工作台那些难懂的词儿,看这篇秒懂!
SVN如何删除文件名包含空格的文件
软件测试岗位巨坑?阿里在职7年测试人告诉你千万别上当
Linux system uninstall, install, upgrade, migrate clickHouse database
Shell脚本实现多选DNS同时批量解析域名IP地址(新更新)
LayaBox---TypeScript---Namespaces and modules
循环结构--do-while循环
LayaBox---TypeScript---迭代器和生成器
零代码工具推荐---HiFlow
LayaBox---TypeScript---Symbols
The 38-year-old daughter is not in love and has no stable job, the old mother is crying
LayaBox---TypeScript---Three slash instructions
Hongxing, donate another million
win10打印服务无法启动(运行时错误automation)