题目:
20. 有效的括号 - 力扣(LeetCode) (leetcode-cn.com)https://leetcode-cn.com/problems/valid-parentheses/
SWUST这道题基本都是仿照力扣这题,并且力扣这道题测试用例给的多一些,我们就采用力扣给的用例场景进行图解。
力扣思路:
SWUSTOJ代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
typedef struct Stack
{
char* data;
int top;//栈顶元素坐标
int capacity;//栈的容量
}ST;
void STInit(ST* ps)
{
ps->data = NULL;
ps->top = 0;//ps->top=-1;
//初始化时top给0,意味着top指向栈顶数据的下一个
//初始化时top给1,意味着top指向栈顶数据
ps->capacity = 0;//初始化容量给0
}
void StackPush(ST* ps, char x)//压栈/入栈操作
{
if (ps->capacity == ps->top)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;//三目操作符
//如果ps->capacity等于0,那么就给4个字节的空间,如果不等于0就扩容两倍
char* tmp = (char*)realloc(ps->data, sizeof(char)*newCapacity);
if (tmp == NULL)//扩容失败退出程序
{
exit(-1);
}
ps->capacity = newCapacity;//把capacity换成新的
ps->data = tmp;//data也指向新的空间
}
ps->data[ps->top] = x;
ps->top++;
}
void StackPop(ST* ps)
{
ps->top--;
}
char StackTop(ST* ps)
{
return ps->data[ps->top - 1];
}
bool StackEmpty(ST* ps)
{
return ps->top == 0;//如果top等于0,返回true
}
int main()
{
char arr[100] = { 0 };
scanf("%s", arr);
getchar();
int n = strlen(arr);
ST List;
STInit(&List);
int flag = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] == '[' || arr[i] == '(')
{
StackPush(&List, arr[i]);
}
else
{
if (StackEmpty(&List))
{
flag = 1;//为空时置为1
break;
}
else if (arr[i] == ']' && StackTop(&List) == '[')
{
StackPop(&List);
}
else if (arr[i] == ')' && StackTop(&List) == '(')
{
StackPop(&List);
}
else
{
flag = 1;//当右括号和左括号不匹配时置为1
break;
}
}
}
if (flag == 1 || List.top != 0)//栈不为空栈时或者flag=1时返回no
printf("NO");
else
printf("YES");
return 0;
}
LeetCode代码:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef char STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;//ps->top=-1
//初始化时top给的是0,意味着top指向栈顶数据的下一个
//初始化时top给的是-1,意味着top指向栈顶数据
ps->capacity = 0;
}
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = realloc(ps->a, sizeof(STDataType)*newCapacity);
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
ps->a = tmp;
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);//assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
bool StackEmpty(ST* ps)
{
assert(ps);
/*if (ps->top == 0)
{
return true;
}
else
{
return false;
}*/
return ps->top == 0;
}
bool isValid(char * s)
{
ST st;
StackInit(&st);
while(*s)
{
if(*s=='('||*s=='{'||*s=='[')
{
StackPush(&st,*s);
s++;
}
else
{
//遇到右括号了,但是栈里面没有数据
//说明前面没有左括号,不匹配,返回false
if(StackEmpty(&st))
{
return false;
}
STDataType top=StackTop(&st);
StackPop(&st);
if((*s=='}'&&top!='{')
||(*s==']'&&top!='[')
||(*s==')'&&top!='('))
{
StackDestroy(&st);
return false;
}
else
{
s++;
}
}
}
//如果栈不是空,说明栈中还有左括号未出栈,没有匹配上,返回的是fasle
bool ret=StackEmpty(&st);
StackDestroy(&st);
return ret;
}