当前位置:网站首页>【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;
}
边栏推荐
- LeetCode 1200.最小绝对差
- TypeScript 基础讲解
- Golang uses context gracefully
- How to adjust bugs in general projects ----- take you through the whole process by hand
- LaMDA 不可能觉醒吗?
- 1040 Longest Symmetric String
- leetcode-6111:螺旋矩阵 IV
- Doing SQL performance optimization is really eye-catching
- 2022年贵州省职业院校技能大赛中职组网络安全赛项规程
- Records of some tools 2022
猜你喜欢
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
Real time clock (RTC)
How to adjust bugs in general projects ----- take you through the whole process by hand
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
Overview of variable resistors - structure, operation and different applications
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
wordpress切换页面,域名变回了IP地址
MySQL advanced part 2: MySQL architecture
Open source storage is so popular, why do we insist on self-development?
随机推荐
Leetcode-556: the next larger element III
[rust notes] 17 concurrent (Part 2)
可变电阻器概述——结构、工作和不同应用
One question per day 2047 Number of valid words in the sentence
一些工具的记录2022
Collection: programming related websites and books
LeetCode 0107.二叉树的层序遍历II - 另一种方法
Some common problems in the assessment of network engineers: WLAN, BGP, switch
Leetcode divide and conquer / dichotomy
[rust notes] 16 input and output (Part 2)
1.14 - 流水线
SPI details
leetcode-6111:螺旋矩阵 IV
A reason that is easy to be ignored when the printer is offline
MySQL advanced part 2: storage engine
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
数据可视化图表总结(一)
How to adjust bugs in general projects ----- take you through the whole process by hand
Binary search template
Introduction and experience of wazuh open source host security solution