力扣第20题有效的括号

cpp 复制代码
typedef char STDataType;
//动态栈
#define allocator_may_return_null 1

typedef struct ST {
    STDataType* _a;
    int _top;//栈顶元素
    int _capacity;//最大容量
}Stack;
//初始化栈
void StackInit(Stack *pst);

//入栈
void StackPush(Stack* pst, STDataType x);

//出栈
void StackPop(Stack* pst);

//获取栈顶元素
STDataType StackTop(Stack* pst);


//判断栈是否为空,是返回1,非空返回0
bool StackEmpty(Stack* pst);

//打印栈
void StackPrint(Stack* pst);

//销毁栈
void StackDestory(Stack* pst);



//初始化栈
void StackInit(Stack* pst)
{
	assert(pst);
	pst->_a = NULL;
	pst->_top = 0;
	pst->_capacity = 0;
}
//入栈
void StackPush(Stack* pst, STDataType x)
{
	assert(pst);
	if (pst->_top == pst->_capacity)
	{
		int newcapacity = pst->_capacity == 0 ? 4 : (pst->_capacity * 2);
		STDataType* temp = (STDataType*)realloc(pst->_a, sizeof(STDataType) * newcapacity);
		if (temp == NULL)
		{
			printf("realloc fail\n");
			exit(1);
		}
		pst->_a = temp;
		pst->_capacity = newcapacity;
	}
	pst->_a[pst->_top] = x;
	pst->_top++;
}


//出栈
void StackPop(Stack* pst)
{
	assert(pst);
	assert(pst->_top > 0);
	pst->_top--;
}

//获取栈顶元素
STDataType StackTop(Stack* pst)
{
	assert(pst);
	assert(pst->_top>0);
	return pst->_a[pst->_top-1];
}

//获取栈的有效元素个数
int StackSize(Stack* pst)
{
	assert(pst);
	return pst->_top;
}

//判断栈是否为空,是返回1,非空返回0
bool StackEmpty(Stack* pst)
{
	assert(pst);
	if (pst->_top == 0)
		return true;
	else
		return false;
}

//打印栈
void StackPrint(Stack* pst)
{
	while (!StackEmpty(pst))
	{
		printf("%d\n", StackTop(pst));
		StackPop(pst);
	}
}


//销毁栈
void StackDestory(Stack* pst)
{
	assert(pst);
	free(pst->_a);
	pst->_a = NULL;
	pst->_top = pst->_capacity = 0;
}
#include "string.h"
bool isValid(char* s) {
    Stack st;
    StackInit(&st);

    
    int n=strlen(s);
    if(n%2!=0)
    return false;

    

    for(int i=0;i<n;i++,s++){
        switch(*s){
            case '(':
            {
                 StackPush(&st,*s);
            }
            break;
            case '[':
            {
                 StackPush(&st,*s);
            }
            break;
            case '{':
            {
                 StackPush(&st,*s);
            }
            break;
            
            case ')':
            {
                if(st._top==0)
                 return false;
                 char m=StackTop(&st);
                 StackPop(&st);
                 if(m!='(')
                 {
                    return false;
                 }
            }
            break;
            case '}':
            {
                if(st._top==0)
                 return false;
                 char m=StackTop(&st);
                 StackPop(&st);
                 if(m!='{')
                 {
                    return false;
                 }
            }
            break;
            case ']':
            {
                if(st._top==0)
                 return false;
                  char m=StackTop(&st);
                 StackPop(&st);
                 if(m!='[')
                 {
                    return false;
                 }
            }
            break;
        }
    }
        
       if(st._top>0)
        {
            return false;
        }
        
        if(StackEmpty(&st)&&*s!='\0')
        return false;
StackDestory(&st);
    return true;
}
相关推荐
铉铉这波能秀6 小时前
LeetCode Hot100数据结构背景知识之元组(Tuple)Python2026新版
数据结构·python·算法·leetcode·元组·tuple
晚霞的不甘6 小时前
Flutter for OpenHarmony 实现计算几何:Graham Scan 凸包算法的可视化演示
人工智能·算法·flutter·架构·开源·音视频
㓗冽6 小时前
60题之内难题分析
开发语言·c++·算法
大江东去浪淘尽千古风流人物6 小时前
【VLN】VLN仿真与训练三要素 Dataset,Simulators,Benchmarks(2)
深度学习·算法·机器人·概率论·slam
铉铉这波能秀6 小时前
LeetCode Hot100数据结构背景知识之字典(Dictionary)Python2026新版
数据结构·python·算法·leetcode·字典·dictionary
蜡笔小马7 小时前
10.Boost.Geometry R-tree 空间索引详解
开发语言·c++·算法·r-tree
我是咸鱼不闲呀7 小时前
力扣Hot100系列20(Java)——[动态规划]总结(下)( 单词拆分,最大递增子序列,乘积最大子数组 ,分割等和子集,最长有效括号)
java·leetcode·动态规划
唐梓航-求职中7 小时前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#
仟濹7 小时前
【算法打卡day3 | 2026-02-08 周日 | 算法: BFS】3_卡码网99_计数孤岛_BFS | 4_卡码网100_最大岛屿的面积DFS
算法·深度优先·宽度优先
Ll13045252987 小时前
Leetcode二叉树part4
算法·leetcode·职场和发展