LeetCode 热题100——栈与队列专题(三)

一、有效的括号

20.有效的括号(题目链接)

思路:

1)括号的顺序匹配:用栈实现,遇到左括号入,遇到右括号出(保证所出的左括号与右括号对应),否则顺序不匹配。

2)括号的数量匹配:

1>左括号大于右括号:用栈实现,遇到左括号入,遇到右括号出,遍历完字符数组,此时栈不为空,则说明左括号数量大于右括号;

2>右括号大于左括号:遇到右括号出时,判断栈是否为空,若此时栈为空,说明右括号数量大于左括号;

typedef char  SDateType;
typedef struct Stack
{SDateType* a;int top;int capacity;}Stack;
//初始化栈和销毁栈
void InitStack(Stack* ps)
{assert(ps);ps->a = NULL;ps->capacity = ps->top = 0;
}
void DestoryStack(Stack* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->capacity = ps->top = 0;}//出栈和入栈
void StackPush(Stack* ps, SDateType x)
{assert(ps);//扩容if (ps->top == ps->capacity){int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;SDateType* tmp = (SDateType*)realloc( ps->a,newcapacity * sizeof(SDateType));if (tmp == NULL){perror("realloc fail:");return;}ps->a = tmp;ps->capacity = newcapacity;}//尾插ps->a[ps->top] = x;ps->top++;
}
void StackPop(Stack* ps)
{assert(ps);assert(ps->top > 0);//只少有一个元素,才能删除ps->top--;
}//栈的有效个数和栈顶元素
int  StackSize(Stack* ps)
{assert(ps);return ps->top;
}
int   StackTop(Stack* ps)
{assert(ps);assert(ps->top > 0);return ps->a[ps->top - 1];
}//栈是否为空
bool StackEmpty(Stack* ps)
{assert(ps);return ps->top == 0;
}
int isValid(char* s) {Stack ps;InitStack(&ps);while (*s){if (*s == '[' || *s == '(' || *s == '{'){StackPush(&ps, *s);s++;}else{if (StackEmpty(&ps)){return false;}char tmp = StackTop(&ps);StackPop(&ps);if ((*s == ']' && tmp != '[') ||(*s == '}' && tmp != '{') ||(*s == ')' && tmp != '(')){return false;}s++;}}if (!StackEmpty(&ps)){return false;}else {return true;}DestoryStack(&ps);
}

二、用队列实现栈 

225. 用队列复制栈(题目链接)

 

思路: 栈是后进先出,队列是先进先出。

用俩队列实现栈

1)入栈时,选择有数据的队列入数据;

2)出栈时,将有数据队列中前k-1个数据出队列,并入到空队列中,返回并出有数据队列的最后一个数据

typedef int QDateType;
typedef struct QueueNode
{QDateType val;struct QueueNode * next;
}QueueNode;
typedef struct Queue
{QueueNode *head;QueueNode *tail;QDateType size;
}Queue;// 初始化队列
void QueueInit(Queue* pq)
{assert(pq);pq->head = pq->tail = NULL;pq->size = 0;
}void QueuePush(Queue* pq, QDateType x)
{assert(pq);QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));node->val = x;node->next = NULL;if (pq->tail == NULL){pq->head = pq->tail = node;pq->size++;}else{pq->tail->next = node;pq->tail = node;pq->size++;}
}
void QueuePop(Queue* pq)
{assert(pq);assert(pq->head != NULL);//只少保证一个节点QueueNode* del = pq->head;pq->head = pq->head->next;free(del);del = NULL;pq->size--;if (pq->head == NULL)//只有一个节点处理{pq->tail = NULL;}
}// 返回队头和队尾
QDateType QueueFront(Queue* pq)
{assert(pq);return pq->head->val;
}
QDateType QueueBack(Queue* pq)
{assert(pq);return pq->tail->val;
}// 获取队列中有效元素个数
int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}bool QueueEmpty(Queue* pq)
{assert(pq);return pq->head==NULL;
}void QueueDestroy(Queue* pq)
{assert(pq);QueueNode* cur = pq->head;while (cur){QueueNode* next = cur->next;free(cur);cur = next;}pq->head = pq->tail = NULL;pq->size = 0;}
typedef struct {Queue q1;Queue q2;} MyStack;MyStack* myStackCreate() {MyStack* obj=(MyStack*)malloc(sizeof(MyStack));QueueInit(&obj->q1);QueueInit(&obj->q2);return obj;}void myStackPush(MyStack* obj, int x) {if(!QueueEmpty(&obj->q1)){QueuePush(&obj->q1,x);}else{QueuePush(&obj->q2,x);}
}
bool myStackEmpty(MyStack* obj) {return QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2);
}int myStackPop(MyStack* obj) {assert(!myStackEmpty(obj));Queue * empty=&obj->q1;Queue * nonempty=&obj->q2;if(!QueueEmpty(empty)){empty=&obj->q2;nonempty=&obj->q1;}while(QueueSize(nonempty)!=1){int tmp= QueueFront(nonempty);QueuePush(empty,tmp);QueuePop(nonempty);}int tmp= QueueFront(nonempty);QueuePop(nonempty);return tmp;
}int myStackTop(MyStack* obj) {assert(!myStackEmpty(obj));Queue * empty=&obj->q1;Queue * nonempty=&obj->q2;if(!QueueEmpty(empty)){empty=&obj->q2;nonempty=&obj->q1;}return QueueBack(nonempty);
}void myStackFree(MyStack* obj) {QueueDestroy(&obj->q1);QueueDestroy(&obj->q2);free(obj);}

三、用栈实现队列 

225. 用栈实现队列(题目链接)

思路: 栈是后进先出,队列是先进先出。

1)入队列:s1栈用来在栈顶入数据;

2)出队列:s2栈用来出栈顶数据,如果s2为空,则从s1出数据入到s2中去(比如;s1中的数据为1,2,3,入到s2变为3,2,1),再出s2的栈顶数据,这样就满足队列的性质了

 

typedef int  SDateType;
typedef struct Stack
{SDateType* a;int top;int capacity;}Stack;
//初始化栈和销毁栈
void InitStack(Stack* ps)
{assert(ps);ps->a = NULL;ps->capacity = ps->top = 0;
}
void DestoryStack(Stack* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->capacity = ps->top = 0;}//出栈和入栈
void StackPush(Stack* ps, SDateType x)
{assert(ps);//扩容if (ps->top == ps->capacity){int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;SDateType* tmp = (SDateType*)realloc( ps->a,newcapacity * sizeof(SDateType));if (tmp == NULL){perror("realloc fail:");return;}ps->a = tmp;ps->capacity = newcapacity;}//尾插ps->a[ps->top] = x;ps->top++;
}
void StackPop(Stack* ps)
{assert(ps);assert(ps->top > 0);//只少有一个元素,才能删除ps->top--;
}//栈的有效个数和栈顶元素
int  StackSize(Stack* ps)
{assert(ps);return ps->top;
}
int   StackTop(Stack* ps)
{assert(ps);assert(ps->top > 0);return ps->a[ps->top - 1];
}//栈是否为空
bool StackEmpty(Stack* ps)
{assert(ps);return ps->top == 0;
}typedef struct {Stack s1;Stack s2;} MyQueue;MyQueue* myQueueCreate() {MyQueue* obj=(MyQueue* )malloc(sizeof(MyQueue));InitStack(&obj->s1);InitStack(&obj->s2);return obj;}void myQueuePush(MyQueue* obj, int x) {StackPush(&obj->s1,x);
}bool myQueueEmpty(MyQueue* obj) {return StackEmpty(&obj->s1)&&StackEmpty(&obj->s2);
}int myQueuePop(MyQueue* obj) {assert(!myQueueEmpty(obj));if(StackEmpty(&obj->s2)){while(!StackEmpty(&obj->s1)){int tmp= StackTop(&obj->s1);StackPush(&obj->s2,tmp);StackPop(&obj->s1);}}int tmp= StackTop(&obj->s2);StackPop(&obj->s2);return tmp;
}int myQueuePeek(MyQueue* obj) {assert(!myQueueEmpty(obj));if(StackEmpty(&obj->s2)){while(!StackEmpty(&obj->s1)){int tmp= StackTop(&obj->s1);StackPush(&obj->s2,tmp);StackPop(&obj->s1);}}return StackTop(&obj->s2);
}void myQueueFree(MyQueue* obj) {DestoryStack(&obj->s1);DestoryStack(&obj->s2);free(obj);}

 四、设计循环队列

622.设计循环队列(题目链接) 

思路一:数组

以front为队列头下标,tail为队列尾下一个元素下标,一共k个数据,开辟k+1个整型大小空间,方便区分队列为空、为满以及一个元素的情况

1)队列为空,front=tail

2)队列为1个元素,front+1=tail

3)   队列为满,(tail+1)%(k+1)==front

 

typedef struct {int *a;int front;int rear;int n;
} MyCircularQueue;MyCircularQueue* myCircularQueueCreate(int k) {MyCircularQueue* obj=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));int * tmp=(int *)malloc(sizeof(int)*(k+1));obj->a=tmp;obj->front=0;obj->rear=0;obj->n=k;return obj;
}bool myCircularQueueIsFull(MyCircularQueue* obj) {if((obj->rear+1)%(obj->n+1)==obj->front){return true;}return false;
}bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {if(myCircularQueueIsFull( obj)){return false;}obj->a[obj->rear]=value;obj->rear++;obj->rear=obj->rear%(obj->n+1);return true;
}bool myCircularQueueIsEmpty(MyCircularQueue* obj) {if(obj->front==obj->rear){return true;}return false;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {if(myCircularQueueIsEmpty( obj)){return false;}obj->front++;obj->front=obj->front%(obj->n+1);return true;
}int myCircularQueueFront(MyCircularQueue* obj) {if(myCircularQueueIsEmpty( obj)){return -1;}return obj->a[obj->front];
}int myCircularQueueRear(MyCircularQueue* obj) {if(myCircularQueueIsEmpty( obj)){return -1;}return  obj->a[(obj->rear-1+obj->n+1)%(obj->n+1)];
}void myCircularQueueFree(MyCircularQueue* obj) {free(obj->a);free(obj);
}

 

思路二:单向循环链表

以head为队列头节点,tail为队列尾尾节点的下一个节点,一共k个数据,开辟k+1个节点的循环单向链表,方便区分队列为空、为满以及一个元素的情况

1)队列为空,head=tail

2)队列为1个元素,head->next=tail

3)   队列为满,tail->next=head

 

typedef struct QueueNode
{int val;struct QueueNode * next;
}QueueNode;QueueNode* BuyNode(int x)
{QueueNode* node=(QueueNode*)malloc(sizeof(QueueNode));node->val=x;node->next=NULL;return node;
}
typedef struct MyCircularQueue{QueueNode *head;QueueNode *tail;QueueNode * pretail;int n;
} MyCircularQueue;MyCircularQueue* myCircularQueueCreate(int k) {MyCircularQueue* obj=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));QueueNode* node=BuyNode(0);obj->pretail=NULL;obj->head=obj->tail=node;obj->n=(k+1);QueueNode* cur=obj->tail;while(k--){QueueNode* node=BuyNode(0);cur->next=node;cur= cur->next;}cur->next=obj->head;return obj;
}bool myCircularQueueIsFull(MyCircularQueue* obj) {if(obj->tail->next==obj->head){return true;}return false;
}bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {if(myCircularQueueIsFull( obj)){return false;}obj->tail->val=value;obj->pretail=obj->tail;obj->tail=obj->tail->next;return true;
}bool myCircularQueueIsEmpty(MyCircularQueue* obj) {if(obj->head==obj->tail){return true;}return false;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {if(myCircularQueueIsEmpty( obj)){return false;}obj->head=obj->head->next;return true;
}int myCircularQueueFront(MyCircularQueue* obj) {if(myCircularQueueIsEmpty( obj)){return -1;}return obj->head->val;
}int myCircularQueueRear(MyCircularQueue* obj) {if(myCircularQueueIsEmpty( obj)){return -1;}return  obj->pretail->val;
}void myCircularQueueFree(MyCircularQueue* obj) {while(obj->n--){QueueNode*next=obj->head->next;free(obj->head);obj->head=next;}obj->head=NULL;free(obj);
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/157061.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Web】Ctfshow XSS刷题记录

目录 反射型XSS ①web316 ②web317-319 ③web320-322 ④web323-326 存储型XSS ①web327 ②web328 ③web329 ④web330 ⑤web331 ⑥web332-333 反射型XSS ①web316 直接输入<script>alert(1)</script>,能弹窗。xss题目一般会有个bot&#xff0c;可以触…

QT 搭建opencv 环境

1. 准备工具CMake 一、CMake介绍 CMake是一个被广泛使用的、开源免费并且完全跨平台的构建工具&#xff0c;可以用简单的语句来描述所有平台的安装(编译过程)。它能够输出各种各样的makefile或者project文件&#xff0c;能测试编译器所支持的C特性&#xff0c;类似UNIX下的aut…

nodejs微信小程序 +python+PHP- 校园志愿者管理系统的设计与实现-计算机毕业设计推荐

目 录 摘 要 I ABSTRACT II 目 录 II 第1章 绪论 1 1.1背景及意义 1 1.2 国内外研究概况 1 1.3 研究的内容 1 第2章 相关技术 3 2.1 nodejs简介 4 2.2 express框架介绍 6 2.4 MySQL数据库 4 第3章 系统分析 5 3.1 需求分析 5 3.2 系统可行性分析 5 3.2.1技术可行性&#xff1a;…

SPSS快速聚类

前言&#xff1a; 本专栏参考教材为《SPSS22.0从入门到精通》&#xff0c;由于软件版本原因&#xff0c;部分内容有所改变&#xff0c;为适应软件版本的变化&#xff0c;特此创作此专栏便于大家学习。本专栏使用软件为&#xff1a;SPSS25.0 本专栏所有的数据文件请点击此链接下…

华为ac+fit漫游配置案例

Ap漫游配置: 其它配置上面一样,ap管理dhcp和业务dhcp全在汇聚交换机 R1: interface GigabitEthernet0/0/0 ip address 11.1.1.1 255.255.255.0 ip route-static 12.2.2.0 255.255.255.0 11.1.1.2 ip route-static 192.168.0.0 255.255.0.0 11.1.1.2 lsw1: vlan batch 100 200…

【通俗易懂】git原理、安装及连接gitlab,github

目录 一、GIT原理【这部分也挺简单&#xff0c;可以看看&#xff0c;如果没时间可以直接跳到第二部分】 SVN与Git的的区别 二、安装Git 2.1 获取Git安装程序 2.2 Git安装过程 三、Git连接Gitlab 3.1 gitlab准备工作 3.2 本地计算机准备工作及配置git 四、Git连接Github…

flutter创建不同样式的按钮,背景色,边框,圆角,圆形,大小都可以设置

在ui设计中&#xff0c;可能按钮会有不同的样式需要你来写出来&#xff0c;所以按钮的不同样式&#xff0c;应该是最基础的功能&#xff0c;在这里我们赶紧学起来吧&#xff0c;web端可能展示有问题&#xff0c;需要优化&#xff0c;但是基本样式还是出来了 我是将所有的按钮放…

【复盘】接口自动化测试框架建设的经验与教训!

为什么选择这个话题&#xff1f; 一是发现很多“点工”在转型迷茫期都会问一些自动化测试相关的问题&#xff0c;可以说自动化测试是“点工”升级的必经之路&#xff1b;二是Google一下接口自动化测试&#xff0c;你会发现很多自动化测试框架相关的文章&#xff0c;但是大部分…

小红书全自动加群引流脚本「 软件工具+引流技术教程」

软件介绍&#xff1a; 小红书群聊最新玩法&#xff0c;可自动检测群人数加群&#xff0c;不会加到垃圾群。定时发送广告&#xff0c;红书群聊的引流玩法回来了 功能一、自动搜索关键词加群&#xff0c;比如创业、项目、鞋子、包包、考公、考研… 功能二、自动检测群人数&…

宏集新闻 | 虹科传感器事业部正式更名为宏集科技

致一直支持“虹科传感器”的朋友们&#xff1a; 为进一步整合资源&#xff0c;给您带来更全面、更优质的服务&#xff0c;我们非常荣幸地宣布&#xff0c;虹科传感器事业部已正式更名为宏集科技。这一重要的改变代表了虹科持续发展进程中的新里程碑&#xff0c;也体现了我们在传…

vue中原生H5拖拽排序_拖拽图片也是同样的道理

原文地址【vue中原生H5拖拽排序_拖拽图片也是同样的道理】 H5有基于拖拽的事件机制&#xff0c;如果你还不熟悉&#xff0c;请看我之前的文章【拖拽上传】中有介绍。 原生拖拽API实现 由于比较简单直接上代码了&#xff1a; <!DOCTYPE html> <html lang"en&qu…

gwas数据获取如何获取完整的GWAS summary数据(1)------GWAS catalog数据库

IEU OpenGWAS project (mrcieu.ac.uk) UK Biobank - UK Biobank GWAS Catalog 在孟德尔随机化&#xff08;Mendelian randomization&#xff0c;MR&#xff09;研究中&#xff0c;对于暴露数据我们只需要那些显著的SNP信息&#xff0c;这样的信息在各种GWAS数据库中都是很容…

C++使用Tensorflow2.6训练好的模型进行预测

要在C语言中调用训练好的TensorFlow模型,需要使用TensorFlow C API。 https://tensorflow.google.cn/install/lang_c?hl=zh-cnten TensorFlow 提供了一个 C API,该 API 可用于为其他语言构建绑定。该 API 在 c_api.h 中定义,旨在实现简洁性和一致性,而不是便利性。 下载…

Jenkins 下载安装

下载 Jenkins 选择Download LTS是稳定版本,尽量选择稳定版本,然后选择你的开发系统. 安装 Jenkins需要JAVA环境&#xff0c;所以安装JAVA环境 Java Jenkins支持17、21等几个版本的Java&#xff0c;OpenJDK JDK 21.0.1 GA Release 安装不要安装到C盘,这个后面会占较大的…

体感互动游戏VR游戏AR体感游戏软件开发

随着科技的不断发展&#xff0c;体感互动游戏正逐渐成为游戏行业的一个重要趋势。这类游戏通过利用传感器、摄像头和运动控制器等技术&#xff0c;使玩家能够通过身体动作与游戏进行实时互动&#xff0c;极大地提升了娱乐体验。 1. 游戏设计与互动元素 体感互动游戏的核心在于…

HTML5生成二维码

H5生成二维码 前言二维码实现过程页面实现关键点全部源码 前言 本文主要讲解如何通过原生HTML、CSS、Js中的qrcodejs二维码生成库&#xff0c;实现一个输入URL按下回车后输出URL。文章底部有全部源码&#xff0c;需要可以自取。 实现效果图&#xff1a; 上述实现效果为&#…

LeetCode209.长度最小的子数组(滑动窗口法、暴力法)

LeetCode209.长度最小的子数组 1.问题描述2.解题思路3.代码4.知识点 1.问题描述 给定一个含有 n 个正整数的数组和一个正整数 target 。找出该数组中满足其总和大于等于 target 的长度最小的 连续子数组 [numsl, numsl1, ..., numsr-1, numsr] &#xff0c;并返回其长度。如果…

C++静态链接库的生成以及使用

目录 一.前言二.生成静态链接库三.使用静态链接库四.其他 一.前言 这篇文章简单讨论一下VS如何生成和使用C静态链接库&#xff0c;示例使用VS2022环境。 二.生成静态链接库 先创建C项目-静态库 然后将默认生成的.h和.cpp文件清理干净&#xff0c;当然你也可以选择保留。 然…

SpringBoot 自动装配原理 - 支付宝支付封装starter

SpringBoot 自动装配 SpringBoot 自动装配原理详细介绍自定义 Spring Boot Starter1.读取配置文件2.注册 AlipayClient bean3.核心代码编写4.注册 AlipayAPI bean5.编写 META-INF/spring.factories 文件6.项目结构测试1.创建一个测试项目&#xff0c;引入自定义 starter 依赖2.…

vue3+elementPlus登录向后端服务器发起数据请求Ajax

后端的url登录接口 先修改main.js文件 // 导入Ajax 前后端数据传输 import axios from "axios"; const app createApp(App) //vue3.0使用app.config.globalProperties.$http app.config.globalProperties.$http axios app.mount(#app); login.vue 页面显示部分…