当前位置:网站首页>[force buckle] bracket matching
[force buckle] bracket matching
2022-07-19 06:26:00 【Patrick star`】
20. Valid parenthesis - Power button (LeetCode)
subject :
Given one only includes '(',')','{','}','[',']' String s , Determines whether the string is valid .
Valid string needs to meet :
Opening parentheses must be closed with closing parentheses of the same type .
The left parenthesis must be closed in the correct order .
Ideas :
Take advantage of the last in first out feature of the stack

Code :
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;
}边栏推荐
- Qtss data type
- 大龄程序员都去哪了?
- [simple and fast] after startup, the desktop is normal, and the taskbar below is unresponsive / the mouse keeps turning
- 计算几何(2)
- [usaco06dec]the fewest coins g (hybrid backpack)
- 嵌入式C语言重点(const、static、voliatile、位运算)
- Positional Change of the Eyeball During Eye Movements: Evidence of Translatory Movement眼球运动过程中眼球的位
- RestAPI实现自动补全 & 案例实现(搜索框输入进行自动补全)
- 【力扣】二叉树的最大深度
- uboot 编译前的配置命令make config分析
猜你喜欢

使用候选选择从人类注视中学习视频显著性

Creation and implementation of WebService interface

2021-11-17 esp32 pin reference

DSL实现Metrics 聚合

Learning video saliency from human gaze using candidate selection

Loadng class `com.mysql.jdbc.Driver‘. This is deprecated. The new driver class is `com.mysql.cj.jdb
![[detailed tutorial installation] [configuration] auxiliary plug-ins about eslint in vscode](/img/d4/31272772b96d3e2f702da74478fd95.png)
[detailed tutorial installation] [configuration] auxiliary plug-ins about eslint in vscode
![Vscode instant English translation plug-in [translation (English Chinese Dictionary)]](/img/f4/9bd90910fef061b423ea8309fab439.png)
Vscode instant English translation plug-in [translation (English Chinese Dictionary)]

Configure the 'log' shortcut key in vscode and remove the console log(‘‘); Semicolon in;

QT creator flashback solution
随机推荐
[USACO06DEC]The Fewest Coins G(混合背包)
嵌入式C语言重点(const、static、voliatile、位运算)
Ehab the Xorcist (异或性质,构造)
Qtss data type
2022/07/12 学习笔记 (day05)循环
LeetCode树
Vscode instant English translation plug-in [translation (English Chinese Dictionary)]
大龄程序员都去哪了?
2022 robocom world robot developer competition - undergraduate group (provincial competition)
2021-09-15
Introduction to basic knowledge of Minio
Simple chrome script automatically skips the charging acknowledgment page after the video playback of station B ends
Lithium battery charging management chip
【力扣】相同的树
Decorate Apple Tree
ES聚合分析报错:“reason“ : “Text fields are not optimised for operations
你见过的最差的程序员是怎样的?
RestAPI实现聚合(黑马教程)
Configure the 'log' shortcut key in vscode and remove the console log(‘‘); Semicolon in;
【力扣】另一棵树的子树