学习时间:
2023年1月21日
题目描述:
题解分享:
// 作 者 : 繁 华 倾 夏
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
// 力扣(LeetCode):20. 有效的括号
// 方法一:顺序栈解法--采用C++的引用接口
// 定义顺序栈
#define MaxSize 50
typedef char ElemType;
typedef struct {
ElemType data[MaxSize];//数组
int top;
}SqStack;
// 初始化栈
void InitStack(SqStack& S)
{
S.top = -1;//代表栈为空
}
// 判断栈是否为空
bool StackEmpty(SqStack& S)
{
if (S.top == -1)
return true;
else
return false;
}
// 入栈
bool Push(SqStack& S, ElemType x)
{
if (S.top == MaxSize - 1)
{
return false;
}
S.data[++S.top] = x;
return true;
}
// 出栈
bool Pop(SqStack& S, ElemType& x)
{
if (-1 == S.top)
return false;
x = S.data[S.top--];
return true;
}
// 获取栈顶元素
bool GetTop(SqStack& S, ElemType& x)
{
if (-1 == S.top)//说明栈为空
return false;
x = S.data[S.top];
return true;
}
bool isValid(char* s) {
SqStack st; // 新建一个栈
InitStack(st); // 初始化栈
ElemType m = 0; // 存储栈顶元素
while (*s) { // 遍历字符数组
if (*s == '(' || *s == '{' || *s == '[') { // 如果是左括号则入栈,栈顶加1
Push(st, *s);
s++;
}
else { // 不是左括号的话,则当前数据为右括号
if (StackEmpty(st)) { // 判断栈是否为空,为空则无可匹配,失败
return false; // 返回函数结束
}
bool top = Pop(st,m); // 取出栈顶元素m
if ((*s == '}' && m != '{') || (*s == ']' && m != '[') || (*s == ')' && m != '(')) {
return false; // 判断左右括号是否匹配,不匹配则返回函数结束
}
else {
s++; // 匹配则进行下一次比较
}
}
}
bool ret = StackEmpty(st); // 当循环结束后,判断栈中是否还有数据,有数据则证明未全部匹配,判空
return ret; // 返回是否为空,程序是否运行到最后
}
/*
// 方法二:链栈解法
// 定义链栈
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;
ps->capacity = 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 = (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++;
}
// 判空栈
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
// 销毁栈
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
// 取出栈顶元素
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
// 取出栈顶元素后,栈顶减1
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
bool isValid(char* s) {
ST st; // 新建一个链栈
StackInit(&st); // 初始化链栈
while (*s) { // 遍历字符数组
if (*s == '(' || *s == '{' || *s == '[') { // 如果是左括号则入栈,栈顶加1
StackPush(&st, *s);
s++;
}
else { // 不是左括号的话,则当前数据为右括号
if (StackEmpty(&st)) { // 判断栈是否为空,为空则无可匹配,失败
StackDestroy(&st); // 销毁栈
return false; // 返回函数结束
}
STDataType top = StackTop(&st); // 取出栈顶元素
StackPop(&st); // 栈顶减1
if ((*s == '}' && top != '{') || (*s == ']' && top != '[') || (*s == ')' && top != '(')) {
StackDestroy(&st); // 判断左右括号是否匹配
return false; // 不匹配则返回函数结束
}
else {
s++; // 匹配则进行下一次比较
}
}
}
bool ret = StackEmpty(&st); // 当循环结束后,判断栈中是否还有数据,有数据则证明未全部匹配,判空
StackDestroy(&st); // 链栈调用完成后,需要还原空间,销毁栈
return ret; // 返回是否为空,程序是否运行到最后
}
*/
// 测试用例
// 输入 s = "()[]{}"
// 输出 true
int main() {
char s[] = "()[]{}";
bool re=isValid(s);
printf("%d", re);
return 0;
}
【繁华倾夏】【每日力扣题解分享】【Day7】