• Leetcode20.有效的括号


    本专栏内容为:leetcode刷题专栏,记录了leetcode热门题目以及重难点题目的详细记录

    💓博主csdn个人主页小小unicorn
    ⏩专栏分类:Leetcode
    🚚代码仓库:小小unicorn的代码仓库🚚
    🌹🌹🌹关注我带你学习编程知识

    题目描述

    题目来源:Leetcode20.有效的括号

    给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。

    有效字符串需满足:

    1.左括号必须用相同类型的右括号闭合。
    2.左括号必须以正确的顺序闭合。
    3.每个右括号都有一个对应的相同类型的左括号。

    解题思路:

    该题是栈的典型应用,满足后进先出的规则(后入栈的前括号将优先与先出现的后括号相匹配)。
     遍历字符串,遇到前括号直接入栈。遇到后括号,判断该后括号与栈顶的前括号是否匹配(若此时栈为空,则字符串无效),若不匹配则字符串无效;若匹配则删除栈顶元素,继续遍历字符串,直到字符串遍历完毕。当字符串遍历完后,检测栈是否为空,若为空,则字符串有效,若不为空,说明有前括号未匹配,字符串无效。

    代码解决:

    #define _CRT_SECURE_NO_WARNINGS
    #include
    #include
    
    //链接:
    //https ://leetcode.cn/problems/valid-parentheses/
    
    typedef char STDataType;//栈中存储的元素类型
    
    typedef struct Stack
    {
    	STDataType* a;//栈
    	int top;//栈顶
    	int capacity;//容量,方便增容
    }Stack;
    
    //初始化栈
    void StackInit(Stack* pst)
    {
    	assert(pst);
    
    	pst->a = (STDataType*)malloc(sizeof(STDataType) * 4);//初始化栈可存储4个元素
    	pst->top = 0;//初始时栈中无元素,栈顶为0
    	pst->capacity = 4;//容量为4
    }
    
    //销毁栈
    void StackDestroy(Stack* pst)
    {
    	assert(pst);
    
    	free(pst->a);//释放栈
    	pst->a = NULL;//及时置空
    	pst->top = 0;//栈顶置0
    	pst->capacity = 0;//容量置0
    }
    
    //入栈
    void StackPush(Stack* pst, STDataType x)
    {
    	assert(pst);
    
    	if (pst->top == pst->capacity)//栈已满,需扩容
    	{
    		STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * pst->capacity * 2);
    		if (tmp == NULL)
    		{
    			printf("realloc fail\n");
    			exit(-1);
    		}
    		pst->a = tmp;
    		pst->capacity *= 2;//栈容量扩大为原来的两倍
    	}
    	pst->a[pst->top] = x;//栈顶位置存放元素x
    	pst->top++;//栈顶上移
    }
    
    //检测栈是否为空
    bool StackEmpty(Stack* pst)
    {
    	assert(pst);
    
    	return pst->top == 0;
    }
    
    //出栈
    void StackPop(Stack* pst)
    {
    	assert(pst);
    	assert(!StackEmpty(pst));//检测栈是否为空
    
    	pst->top--;//栈顶下移
    }
    
    //获取栈顶元素
    STDataType StackTop(Stack* pst)
    {
    	assert(pst);
    	assert(!StackEmpty(pst));//检测栈是否为空
    
    	return pst->a[pst->top - 1];//返回栈顶元素
    }
    
    //获取栈中有效元素个数
    int StackSize(Stack* pst)
    {
    	assert(pst);
    
    	return pst->top;//top的值便是栈中有效元素的个数
    }
    /*---以上代码是栈的基本功能实现,以下代码是题解主体部分---*/
    
    
    
    bool isValid(char* s)
    {
    	Stack st;//创建一个栈
    	StackInit(&st);//初始化栈
    
    	char* cur = s;
    	while (*cur)
    	{
    		//前括号统一进栈
    		if (*cur == '(' || *cur == '[' || *cur == '{')
    		{
    			StackPush(&st, *cur);
    			cur++;
    		}
    		else
    		{
    			//若遇到后括号,且栈为空,则字符串无效
    			if (StackEmpty(&st))
    			{
    				StackDestroy(&st);
    				return false;
    			}
    			//获取栈顶元素
    			char top = StackTop(&st);
    			//后括号与栈顶的前括号不匹配
    			if (top == '(' && *cur != ')' || top == '{' && *cur != '}' || top == '[' && *cur != ']')
    			{
    				StackDestroy(&st);
    				return false;
    			}
    			//匹配
    			else
    			{
    				StackTop(&st);
    				cur++;
    			}
    		}
    	}
    	//检测栈是否为空
    	bool ret = StackEmpty(&st);
    	StackDestroy(&st);
    	//返回结果
    	return ret;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138

    结果与总结:

    在这里插入图片描述

    通过所有示例,问题得到解决。

  • 相关阅读:
    如何在revit中管理CAD的图层?
    React常见的一些坑
    详解 BAT 面试必问之 ThreadLocal(源码 + 内存)
    openGauss的索引组织表
    Java过滤器Filter讲解
    Springboot项目 配置热部署
    [ Linux Busybox ] getopt32 命令解析
    苹果服务器退款通知处理notificationType为CONSUMPTION_REQUEST
    使用cmake,将github上的某一个库进行集成到vs2022上
    Firefly RK3399 PC pro Android 10下载验证
  • 原文地址:https://blog.csdn.net/weixin_72066135/article/details/133822640