当前位置:网站首页>【LeetCode】Easy | 20. Valid parentheses
【LeetCode】Easy | 20. Valid parentheses
2022-07-05 06:15:00 【XiYang-DING】
subject :
Ideas :
Pictured
Code :
typedef char STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
// initialization
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
// Push
void StackPush(ST* ps, STDataType x)
{
assert(ps);
// Judge whether there is enough space
// The space is insufficient
if (ps->top == ps->capacity)
{
int newCapacity = (ps->capacity == 0) ? 4 : ps->capacity * 2;
STDataType* tmp = realloc(ps->a, sizeof(STDataType) * newCapacity);
// Failed to open up
if (tmp == NULL)
{
printf("realloc fail!");
exit(0);
}
// It's a success
ps->a = tmp;
ps->capacity = newCapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
// Out of the stack
void StackPop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
ps->top--;
}
// Take the top element of the stack
STDataType StackTop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
// Judge whether the stack is empty
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
// Destruction of the stack
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
bool isValid(char * s){
ST st;
StackInit(&st);
while(*s)
{
if(*s=='{'|| *s=='[' || *s=='(')
{
StackPush(&st,*s);
s++;
}
else
{
if(StackEmpty(&st))
{
StackDestroy(&st);
return false;
}
char topElem=StackTop(&st);
StackPop(&st);
if((topElem == '('&&*s != ')')
||(topElem == '{'&&*s != '}')
||(topElem == '['&&*s != ']')
)
{
StackDestroy(&st);
return false;
}
else{
s++;
}
}
}
if(StackEmpty(&st))
{
return true;
}
return false;
}
边栏推荐
- QQ电脑版取消转义符输入表情
- Quickly use Amazon memorydb and build your own redis memory database
- MySQL advanced part 2: optimizing SQL steps
- 7. Processing the input of multidimensional features
- leetcode-6111:螺旋矩阵 IV
- Daily question 2006 Number of pairs whose absolute value of difference is k
- 剑指 Offer II 058:日程表
- Daily question 1688 Number of matches in the competition
- “磐云杯”中职网络安全技能大赛A模块新题
- Appium基础 — 使用Appium的第一个Demo
猜你喜欢
Leetcode-6110: number of incremental paths in the grid graph
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
Appium自动化测试基础 — Appium测试环境搭建总结
开源存储这么香,为何我们还要坚持自研?
Doing SQL performance optimization is really eye-catching
数据可视化图表总结(一)
SPI 详解
1.13 - RISC/CISC
Appium automation test foundation - Summary of appium test environment construction
随机推荐
做 SQL 性能优化真是让人干瞪眼
Regulations for network security events of vocational group in 2022 Guizhou Vocational College skill competition
数据可视化图表总结(二)
MySQL advanced part 1: stored procedures and functions
leetcode-31:下一个排列
【Rust 笔记】15-字符串与文本(下)
对for(var i = 0;i < 5;i++) {setTimeout(() => console.log(i),1000)}的深入分析
SQLMAP使用教程(二)实战技巧一
1.15 - input and output system
wordpress切换页面,域名变回了IP地址
Introduction to LVS [unfinished (semi-finished products)]
leetcode-6111:螺旋矩阵 IV
1040 Longest Symmetric String
One question per day 1020 Number of enclaves
[rust notes] 17 concurrent (Part 2)
Data visualization chart summary (II)
Full Permutation Code (recursive writing)
剑指 Offer II 058:日程表
Basic explanation of typescript
MIT-6874-Deep Learning in the Life Sciences Week 7