当前位置: 首页 > article >正文

数据结构:有效的括号(OJ20)

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

有效字符串需满足:

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

示例 1:

输入:s = "()"

输出:true

示例 2:

输入:s = "()[]{}"

输出:true

示例 3:

输入:s = "(]"

输出:false

示例 4:

输入:s = "([])"

输出:true

提示:

  • 1 <= s.length <= 104
  • s 仅由括号 '()[]{}' 组成
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>

typedef char STDataType;
typedef struct Stack
{
	STDataType* _a;
	int _top;		// 栈顶
	int _capacity;  // 容量 
}Stack;

// 初始化栈 
void StackInit(Stack* ps)
{
	assert(ps);
	ps->_a = NULL;
	ps->_capacity = ps->_top = 0;
}

// 入栈 
void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	if (ps->_capacity == ps->_top)
	{
		int newcapacity = ps->_capacity == 0 ? 4 : 2 * ps->_capacity;
		STDataType* tmp = (STDataType*)realloc(ps->_a, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->_a = tmp;
		ps->_capacity = newcapacity;
	}
	//空间足够
	ps->_a[ps->_top++]=data;
}

// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
bool StackEmpty(Stack* ps)
{
	assert(ps);
	return ps->_top==0;
}

// 出栈 
void StackPop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	--ps->_top;
}

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

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


// 销毁栈 
void StackDestroy(Stack* ps)
{
	assert(ps);
	if (ps->_a)
	{
		free(ps->_a);
	}
	ps->_a = NULL;
	ps->_capacity = ps->_top = 0;
}

bool isValid(char* S)
{
	Stack st;
	StackInit(&st);
	//遍历字符串
	char* ps = S;
	while (*ps != '\0')
	{
		if (*ps == '(' || *ps == '[' || *ps == '{')
		{
			StackPush(&st,*ps);
		}
		else
		{
			//右括号和栈顶元素比较
			if (StackEmpty(&st))
			{
				return false;
			}
			//栈不为空才取栈顶元素比较
			char ch = StackTop(&st);
			if ((*ps == ')' && ch == '(')
				|| (*ps == ']' && ch == '[')
				|| (*ps == '}' && ch == '{'))
			{
				StackPop(&st);
			}
			else//不匹配
			{
				StackDestroy(&st);
				return false;
			}
		}
		ps++;
	}
	bool ret = StackEmpty(&st) == true;
	//StackDestroy(&st);

	return ret;
}

int main()
{
	Stack ST;
	//StackInit(&ST);
	//StackPush(&ST, 1);
	//StackPush(&ST, 2);
	//StackPush(&ST, 3);
	//isValid("(nihao}{}");
	char input[] = "[]{}()";
	bool result = isValid(input);
	printf("%s has valid parentheses\n",input);
	//while (!StackEmpty(&ST))
	//{
	//	STDataType data = StackTop(&ST);
	//	printf("%s ", *&data);
	//	//出栈
	//	StackPop(&ST);
	//}
	//StackDestroy(&ST);
	return 0;
}


http://www.kler.cn/news/336743.html

相关文章:

  • 用友U8+CRM leadconversion.php SQL注入复现
  • 2024/10/1 408大题专训之磁盘管理
  • Linux安装部署MySQL8.0加遇着问题解决
  • rabbitmq 2024第38-39 事务消息 尚硅谷答疑
  • Hbase要点简记
  • windows查看端口占用情况并终结该进程
  • 【C++掌中宝】类和对象(二):隐藏的this指针
  • 基于Zynq SDIO WiFi移植三(支持2.4/5G)
  • clickhouse数据字典
  • 《Linux从小白到高手》理论篇:Linux的系统环境管理
  • MicoZone-Maven
  • Ubuntu2404安装
  • 15分钟学 Python 第40天:Python 爬虫入门(六)第一篇
  • Android SystemUI组件(11)SystemUIVisibility解读
  • 头歌 | 获取最多金币
  • rabbitmq----数据管理模块
  • 智能视界·大模型驱动视频矩阵管理系统
  • Anaconda的安装与环境设置
  • Android车载——VehicleHal初始化(Android 11)
  • MATLAB计算与建模常见函数:2.回归模型