链接:力扣
解析:*s为左括号就进栈, *s为右括号就让栈里的栈顶元素出栈,如果栈顶元素和*s不匹配那就return false,如果栈顶元素和*s匹配那就pop掉栈顶元素,并使s++直到*s='\0',而且要stackempty判断栈为空为止,若结束后栈内为空则return true; 若不为空,如例子*s="(",此时栈里有"(",但*s='\0'了,此例子仍然是错的,要return false;
本人版本:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;//法1.初始化top=-1;则top先++,再放入元素到top处,此时放完最后一个元素后,top等于最后一个元素的下标,即栈顶的下标,此时的top+1才等于capacity;
//法2.初始化top=0;则先放元素到top处,再top++;此时放完最后一个元素,也就是放完栈顶的那个元素后,top等于栈顶(最后的那个元素)的下标+1,此时的top=capacity;
int capacity;
}ST;
STDataType StackTop(ST* ps)
{
assert(ps);
return ps->a[ps->top - 1];
}
ST* CheckCapactiy(ST* ps)
{
if (ps->top == ps->capacity)
{
STDataType* tmp = NULL;
int NewCapacity = ps->capacity == 0 ? 4 : 2 * (ps->capacity);
tmp = (STDataType*)realloc(ps->a, sizeof(STDataType)*NewCapacity);
if (tmp != NULL)
{
ps->a = tmp;//!!!注意是给ps->a开辟的空间,而不是给ps
ps->capacity = NewCapacity;
printf("扩容成功\n");
return ps;//!!!记得要把ps return 回去
}
else
{
printf("扩容失败\n");
perror("CheckCapacity::realloc");
exit(-1);
}
}
return ps;
}
void StackPush(ST* ps, STDataType x)
{
ps = CheckCapactiy(ps);
ps->a[ps->top] = x;
ps->top++;
}
void StackPop(ST* ps)
{
if (ps->top <= 0)
{
printf("栈为空\n");
return;
}
else
{
ps->top--;
}
}
void StackDestroy(ST* ps)
{
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
bool StackEmpty(ST* ps)//若为空则返回1;若不为空则返回负的随机值
{
return ps->top == 0;
}
int StackSize(ST* ps)
{
return ps->top - 1;
}
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;
}
STDataType top = StackTop(&st);
StackPop(&st);
if ((*s == ')' && top == '(') || (*s == '}' && top == '{') || (*s == ']' && top == '['))
{
s++;
}
else
{
StackDestroy(&st);
return false;
}
}
}
bool ret = StackEmpty(&st);
StackDestroy(&st);
return ret;
}
答案版本:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;//法1.初始化top=-1;则top先++,再放入元素到top处,此时放完最后一个元素后,top等于最后一个元素的下标,即栈顶的下标,此时的top+1才等于capacity;
//法2.初始化top=0;则先放元素到top处,再top++;此时放完最后一个元素,也就是放完栈顶的那个元素后,top等于栈顶(最后的那个元素)的下标+1,此时的top=capacity;
int capacity;
}ST;
STDataType StackTop(ST* ps)
{
assert(ps);
return ps->a[ps->top-1];
}
ST* CheckCapactiy(ST* ps)
{
if(ps->top==ps->capacity)
{
STDataType* tmp = NULL;
int NewCapacity = ps->capacity == 0 ? 4 : 2 * (ps->capacity);
tmp = (STDataType*)realloc(ps->a, NewCapacity);
if (tmp != NULL)
{
ps->a = tmp;//!!!注意是给ps->a开辟的空间,而不是给ps
ps->capacity = NewCapacity;
printf("扩容成功\n");
return ps;//!!!记得要把ps return 回去
}
else
{
printf("扩容失败\n");
perror("CheckCapacity::realloc");
exit(-1);
}
}
return ps;
}
void StackPop(ST* ps)
{
if (ps->top <= 0)
{
printf("栈为空\n");
return;
}
else
{
ps->top--;
}
}
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)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 StackDestroy(ST* ps)
{
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
bool StackEmpty(ST* ps)//若为空则返回1;若不为空则返回负的随机值
{
return ps->top == 0;
}
int StackSize(ST* ps)
{
return ps->top - 1;
}
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;
}
STDataType top=StackTop(&st);
StackPop(&st);
if ((*s== ')'&& top == '(') || (*s == '}' && top == '{') || (*s == ']' && top == '['))
{
s++;
}
else
{
StackDestroy(&st);
return false;
}
}
}
bool ret=StackEmpty(&st);
StackDestroy(&st);
return ret;
}