• 【LeetCode刷题(数据结构与算法)】:有效的括号


    在这里插入图片描述
    首先这里需要用到栈的知识 力扣官方会有相关的栈的实现的接口函数 所以我们这里就直接拷贝一份我们栈的实现的代码

    typedef int STDataType;
    
    typedef struct Stack
    {
    	STDataType* a;
    	int top;
    	int capacity;
    }ST;
    
    void STInit(ST* ps)
    {
    	assert(ps);
    	ps->a = NULL;
    	ps->capacity = 0;
    	ps->top = 0;
    }
    
    void STDestroy(ST* ps)
    {
    	assert(ps);
    	free(ps->a);
    	ps->top = ps->capacity = 0;
    }
    
    void STPush(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)
    		{
    			perror("realloc fail");
    			exit(-1);
    		}
    		ps->a = tmp;
    		ps->capacity = newCapacity;
    	}
    	ps->a[ps->top] = x;
    	ps->top++;
    }
    
    void STPop(ST* ps)
    {
    	assert(ps);
    	assert(ps->top > 0);
    	--ps->top;
    }
    
    STDataType STTop(ST* ps)
    {
    	assert(ps);
    	assert(ps->top > 0);
    	return ps->a[ps->top - 1];
    }
    
    STDataType STSize(ST* ps)/*.....*/
    {
    	assert(ps);
    	return ps->top;
    }
    
    bool STEmpty(ST* ps)
    {
    	assert(ps);
    	return ps->top == 0;//0;
    }
    
    
    • 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

    判断括号的有效性可以使用「栈」这一数据结构来解决
    我们遍历给定的字符串 s。当我们遇到一个左括号时,我们会期望在后续的遍历中,有一个相同类型的右括号将其闭合。由于后遇到的左括号要先闭合,因此我们可以将这个左括号放入栈顶。
    当我们遇到一个右括号时,我们需要将一个相同类型的左括号闭合。此时,我们可以取出栈顶的左括号并判断它们是否是相同类型的括号。如果不是相同的类型,或者栈中并没有左括号,那么字符串 s 无效,返回 False。为了快速判断括号的类型,我们可以使用哈希表存储每一种括号。哈希表的键为右括号,值为相同类型的左括号
    在遍历结束后,如果栈中没有左括号,说明我们将字符串 s 中的所有左括号闭合,返回True,否则返回 False
    注意到有效字符串的长度一定为偶数,因此如果字符串的长度为奇数,我们可以直接返回 False,省去后续的遍历判断过程

    typedef int STDataType;
    
    typedef struct Stack
    {
    	STDataType* a;
    	int top;
    	int capacity;
    }ST;
    
    void STInit(ST* ps)
    {
    	assert(ps);
    	ps->a = NULL;
    	ps->capacity = 0;
    	ps->top = 0;
    }
    
    void STDestroy(ST* ps)
    {
    	assert(ps);
    	free(ps->a);
    	ps->top = ps->capacity = 0;
    }
    
    void STPush(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)
    		{
    			perror("realloc fail");
    			exit(-1);
    		}
    		ps->a = tmp;
    		ps->capacity = newCapacity;
    	}
    	ps->a[ps->top] = x;
    	ps->top++;
    }
    
    void STPop(ST* ps)
    {
    	assert(ps);
    	assert(ps->top > 0);
    	--ps->top;
    }
    
    STDataType STTop(ST* ps)
    {
    	assert(ps);
    	assert(ps->top > 0);
    	return ps->a[ps->top - 1];
    }
    
    STDataType STSize(ST* ps)/*.....*/
    {
    	assert(ps);
    	return ps->top;
    }
    
    bool STEmpty(ST* ps)
    {
    	assert(ps);
    	return ps->top == 0;//0;
    }
    
    bool isValid(char * s){
        ST st;
        STInit(&st);//创建一个栈
        char top;
        while(*s)
        {
            switch(*s)
            {
                case '(':
                case '[':
                case '{':
                STPush(&st,*s);
                break;
                case ']':
                if(STEmpty(&st))
                {
                    return false;
                }
                top=STTop(&st);
                STPop(&st);
                if(top!='[')
                {
                    return false;
                }   
                break;         
                case ')':
                if(STEmpty(&st))
                {
                    return false;
                }
                top=STTop(&st);
                STPop(&st);
                if(top!='(')
                {
                    return false;
                }   
                break;   
                case '}':
                if(STEmpty(&st))
                {
                    return false;
                }
                top=STTop(&st);
                STPop(&st);
                if(top!='{')
                {
                    return false;
                }   
                break; 
            }
            s++;
        }
        //顺序和数量都匹配就返回ret-true
        bool ret=STEmpty(&st);
        STDestroy(&st);//销毁栈
        return ret;//true
    }
    
    • 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
  • 相关阅读:
    CentOS修改主机名
    LeetCode 209. 长度最小的子数组
    基于pytorch实现Resnet对本地数据集的训练
    【LeetCode】不同的子序列 II [H](动态规划)
    MySQL 8.2 Command Line Client打开时一闪而过闪退问题
    C#中各种循环遍历的功能与应用
    “遥感新纪元:GPT技术引领地球观测的智慧革新“
    MySQL限制登陆失败次数配置
    磁性机器人在医学领域取得进展
    Spring MVC介绍
  • 原文地址:https://blog.csdn.net/fjj2397194209/article/details/133882273