每日一练——有效的括号

20. 有效的括号 - 力扣(LeetCode)

错误记录

cpp 复制代码
#include<stddef.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef char STDataType;

typedef struct Stack
{
	STDataType* a;
	int capacity;
	int top;
} Stack;

void STInit(Stack* st);
void STDestroy(Stack* st);

void STPush(Stack* st, STDataType x);
STDataType STPop(Stack* st);
STDataType STTop(Stack* st);
int STSize(Stack* st);
bool STEmpty(Stack* st);

void STInit(Stack* st)
{
	assert(st);
	st->a = NULL;
	st->capacity = st->top = 0;
}

void STDestroy(Stack* st)
{
	assert(st);
	free(st->a);
	st->a = NULL;
	st->capacity = st->top =  0;
}

void STPush(Stack* st, STDataType x)
{
	assert(st);
	//checkcapacity
	if (st->capacity == st->top)
	{
		int newcapacity = st->capacity == 0 ? 4 : st->capacity * 2;
		STDataType* temp = (STDataType*)realloc(st->a, sizeof(STDataType) * newcapacity);
		if (NULL == temp)
		{
			perror("realloc failed");
			exit(-1);
		}
		st->a = temp;
		st->capacity = newcapacity;
	}
	st->a[st->top] = x;
	++st->top;
}

STDataType STPop(Stack* st)
{
	assert(st);
	assert(st->top);
	STDataType ret = STTop(st);
	--st->top;
	return ret;
}

STDataType STTop(Stack* st)
{
	assert(st);
	assert(st->top);
	return st->a[st->top - 1];
}

int STSize(Stack* st)
{
	return st->top;
}

bool STEmpty(Stack* st)
{
	return st->top == 0;
}


bool isValid(char* s)
{
    Stack st;
    STInit(&st);
    int i = 0;
    while(*s)
    {
        if(*s == '(' || *s == '[' || *s == '{')
            STPush(&st, *s);
        else
        {
            if((STPop(&st) == '(' && *s != ')') ||
            (STPop(&st) == '[' && *s != ']') ||
            (STPop(&st) == '{' && *s != '}'))
            {
                STDestroy(&st);
                return false;
            }
        }
        ++s;
    }
    STDestroy(&st);
    return true;
}

这里比较三次直接出了3次栈,应该只出一次比较三次。

修改成下面这样:

之后在" ( "和" ) "又分别翻车了,加了下面两句判断。

最终提交

cpp 复制代码
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>

typedef char STDataType;

typedef struct Stack {
    STDataType* a;
    int capacity;
    int top;
} Stack;

void STInit(Stack* st);
void STDestroy(Stack* st);

void STPush(Stack* st, STDataType x);
STDataType STPop(Stack* st);
STDataType STTop(Stack* st);
int STSize(Stack* st);
bool STEmpty(Stack* st);

void STInit(Stack* st) {
    assert(st);
    st->a = NULL;
    st->capacity = st->top = 0;
}

void STDestroy(Stack* st) {
    assert(st);
    free(st->a);
    st->a = NULL;
    st->capacity = st->top = 0;
}

void STPush(Stack* st, STDataType x) {
    assert(st);
    // checkcapacity
    if (st->capacity == st->top) {
        int newcapacity = st->capacity == 0 ? 4 : st->capacity * 2;
        STDataType* temp =
            (STDataType*)realloc(st->a, sizeof(STDataType) * newcapacity);
        if (NULL == temp) {
            perror("realloc failed");
            exit(-1);
        }
        st->a = temp;
        st->capacity = newcapacity;
    }
    st->a[st->top] = x;
    ++st->top;
}

STDataType STPop(Stack* st) {
    assert(st);
    assert(st->top);
    STDataType ret = STTop(st);
    --st->top;
    return ret;
}

STDataType STTop(Stack* st) {
    assert(st);
    assert(st->top);
    return st->a[st->top - 1];
}

int STSize(Stack* st) { return st->top; }

bool STEmpty(Stack* st) { return st->top == 0; }

bool isValid(char* s) {
    Stack st;
    STInit(&st);
    int i = 0;
    while (*s) {
        if (*s == '(' || *s == '[' || *s == '{')
            STPush(&st, *s);
        else
        {
            if(STEmpty(&st))
            {
                STDestroy(&st);
                return false;
            }
            char topval = STPop(&st);
            if ((topval == '(' && *s != ')') ||
                (topval == '[' && *s != ']') ||
                (topval == '{' && *s != '}'))
            {
                STDestroy(&st);
                return false;
            }
        }
        ++s;
    }
    if(!STEmpty(&st)) return false;
    STDestroy(&st);
    return true;
}

修改日志

2024/6/12

一处return的时候忘记调用STDestroy,有内存泄漏风险,补上了。

把变量top名称改成了topval,避免与Stack的成员top混杂,增强代码可读性。

相关推荐
归寻太乙5 分钟前
Linux环境变量
linux·运维·服务器
m0Java门徒6 分钟前
面向对象编程核心:封装、继承、多态与 static 关键字深度解析
java·运维·开发语言·intellij-idea·idea
Sapphire~12 分钟前
Linux-06 ubuntu 系统截图软件使用简单记录
linux·运维·ubuntu
高级IT技术专家secops99820 分钟前
在统信UOS/麒麟Kylin OS操作系统中配置APT和GIT代理
运维·服务器·git·系统安全·kylin
工头阿乐36 分钟前
Appium自动化开发环境搭建
运维·appium·自动化
自由鬼38 分钟前
开源漏洞扫描器:OpenVAS
运维·服务器·安全·网络安全·开源·漏洞管理
難釋懷40 分钟前
Shell脚本-while循环语法结构
linux·运维·服务器·bash
B64A-消闲1 小时前
shell命令一
linux·运维
biter00881 小时前
ubuntu(28):ubuntu系统多版本conda和多版本cuda共存
linux·人工智能·ubuntu·conda
兜小糖的小秃毛1 小时前
两段文本比对,高亮出差异部分
linux·前端·javascript