#define STACK_SIZE 10000
struct stack{
char a[STACK_SIZE];
int top;
};
void push(struct stack *s, char c)
{
if(s->top == STACK_SIZE-1)
return;
else{
s->top++;
s->a[s->top] = c;
}
}
char pop(struct stack *s)
{
if(s->top == -1)
return -1;
else{
return s->a[s->top--];
}
}
bool isValid(char* s) {
struct stack stack;
stack.top = -1;
char c;
int tag = 0;
while(*s != '\0'){
printf("%c tag = %d\n", *s, tag);
if(*s == '(' || *s == '{' || *s == '['){
push(&stack, *s);
s++;
tag++;
}
else if(*s == ')' || *s == '}' || *s == ']'){
c = pop(&stack);
printf("c = %c\n", c);
if(c == '(' && *s == ')')
tag--,s++;
else if(c == '{' && *s == '}')
tag--,s++;
else if(c == '[' && *s == ']')
tag--,s++;
else
return false;
}else
s++;
}
if(tag == 0)
return true;
else
return false;
}
20. Valid Parentheses
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- Given a string containing just the characters '(', ')', '...
- Problem:### Given a string containing just the characters...
- 题目 Given a string containing just the characters '(', ')'...