数据结构刷题

空间复杂度:临时开辟的空间、空间是可以重复利用的

递归为O(n)

时间复杂度:程序执行次数

消失的数字

力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

思路1:利用连续的特点求等差和然后减去所有元素得到的就是消失的数字

时间复杂度:O(n)     空间复杂度:O(1)

思路2:将给定数组的元素和连续的元素进行异或(异或与顺序无关),相同元素异或结果为0。会得到有一个落单的数字与0异或就是这个数字。

时间复杂度:O(n)     空间复杂度:O(1)

第一种方法大家自行实现,我们可以实现一下不常见的第二种方法:

int missingNumber(int* nums, int numsSize){int x = 0;//0与任何数异或为该数for(int i = 0;i<numsSize;i++){x ^= nums[i];}for(int i = 0;i<=numsSize;i++){x ^= i;}return x;
}

轮转数组

力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

c语言阶段曾实现了两种方法,一种是依次移动,移动M次。一种是局部逆置和整体逆置。前者时间复杂度为O(N*M),后者为O(N)

空间换时间:创建一个临时数组,前面放需要旋转的数据,后面放没有旋转的数据即可。 

不完整代码: 

void rotate(int* nums, int numsSize, int k){int tmp[numsSize];k %= numsSize;//循环次数不必多于元素个数int i = 0;int j = 0;for(i=numsSize-k;i<numsSize;i++){tmp[j] = nums[i];j++;}for(i = 0;i<numsSize-k;i++){tmp[j] = nums[i];
}
for//打印
}

有效的括号

力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

思路:让左括号依次入栈,与右括号匹配。

匹配前:判断栈是否为空,为空说明无左括号。返回false。

匹配中:匹配涉及多次如 " () {} (())",记录指针位置与栈顶比较,然后出栈,不匹配返回false。

匹配结束栈空为匹配成功,否则匹配失败。

注意返回前都需进行销毁操作。

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef char STDatatype;
typedef struct Stack
{STDatatype* a;int capacity;int top;
}ST;
void check_capacity(ST* ps)
{if(ps->capacity == ps->top){int newCapacity = 0 ? 4:ps->capacity*2;char* tmp;tmp = (STDatatype*)realloc(ps->a,sizeof(STDatatype) * newCapacity);if (tmp == NULL){perror("malloc");exit(-1);}ps->a = tmp;ps->capacity = newCapacity;}
}
bool StackEmpty(ST* ps)//判空
{assert(ps);return ps->top == 0;
}
STDatatype StackTop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));return ps->a[ps->top-1];//top为最后一个元素的下一个
}
void StackDestroy(ST* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->top = ps->capacity = 0;
}
void StackInit(ST* ps)
{/*ps->a = NULL;ps->top = 0;ps->capacity = 0;*///初始化空间ps->a = (STDatatype*)malloc(sizeof(STDatatype) * 4);if (ps->a == NULL){perror("malloc");exit(-1);}ps->top = 0;ps->capacity = 4;
}
void StackPop(ST* ps)//出栈
{assert(ps);//if(ps->top>0)assert(!StackEmpty(ps));ps->top--;
}
void StackPush(ST* ps, STDatatype x)
{assert(ps);check_capacity(ps);ps->a[ps->top] = x;ps->top++;//指向下一个
}
bool isValid(char* s) {ST ps;StackInit(&ps); 
while(*s)
{if(*s == '(' || *s == '[' ||*s == '{' ){StackPush(&ps,*s);s++;}else{if(StackEmpty(&ps))//如果没有左括号,栈顶没有元素{StackDestroy(&ps);//注意返回前都要手动释放空间return false;}char top = StackTop(&ps);if((*s == ')' && top != '(') || (*s == ']' && top != '[') ||(*s == '}' && top != '{'))//{StackDestroy(&ps);return false;}else{StackPop(&ps);s++;}}
}
bool ret = StackEmpty(&ps);
StackDestroy(&ps);
return ret;
}

用队列实现栈

力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

先导入我们实现过的队列:

#pragma once
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int QDataType;
typedef struct QueueNode
{QDataType data;struct QueueNode* next;
}QNode;
struct Queue
{QNode* head;QNode* tail;int size;
};
void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
bool QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->head;while (cur){QNode* Next =cur->next;free(cur);cur = Next;}pq->head = pq->tail = NULL;//避免野指针pq->size = 0;
}
void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");exit(-1);}   newnode->data = x;newnode->next = NULL;pq->size++;if (pq->head == NULL){pq->head = pq->tail = newnode;}else{pq->tail->next = newnode;pq->tail = newnode;}
}
void QueuePop(Queue* pq)
{//出队头删assert(pq);assert(pq->head);QNode* del = pq->head;pq->head = pq->head->next;free(del);if (pq->head == NULL)pq->tail = NULL;pq->size--;}
void QueueInit(Queue* pq)
{assert(pq);pq->head = NULL;pq->tail = NULL;pq->size = 0;
}
bool QueueEmpty(Queue* pq)
{assert(pq);return pq->head == NULL && pq->tail == NULL;
}
int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}
QDataType QueueFront(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->head->data;
}
QDataType QueueBack(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->tail->data;
}

 两个队列

将两个队列封装在一个结构体中。

typedef struct Queue
{QNode* head;QNode* tail;int size;
}Queue;//简写

创建并初始化链表(栈)

注意要想保证我们创建的结构体生命周期能够贯彻整个工程,必须在上申请空间。

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);}elseQueuePush(&obj->q2,x);
}

模拟出栈

出栈是在队尾进行出(后进先出),我们将非空队列进行出队只剩下最后一个元素即是栈顶的元素,然后将其保存后出栈避免造成内存泄露,然后返回。

nt myStackPop(MyStack* obj) {//假定一方不为空Queue* empty = &obj->q1;Queue* nonempty = &obj->q2;if(!QueueEmpty(empty)){empty = &obj->q2;nonempty = &obj->q1;}while(QueueSize(nonempty) > 1)//O(1)导入空来链表{QueuePush(empty,QueueFront(nonempty));//非空取队头插入空 QueuePop(nonempty);}//保证原链表返回为空int top = QueueFront(nonempty);QueuePop(nonempty);//出栈return top;
}

模拟栈顶

直接返回队尾元素即可。

int myStackTop(MyStack* obj) {//返回队尾if(!QueueEmpty(&obj->q1)){return QueueBack(&obj->q1);}elsereturn QueueBack(&obj->q2);	
}

模拟栈判空

bool myStackEmpty(MyStack* obj) {return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}

模拟栈销毁

除了释放队列指针组合的结构体外,还要将每个队列里的元素释放。

void myStackFree(MyStack* obj) {QueueDestroy(&obj->q1);QueueDestroy(&obj->q2);free(obj);
}

用栈实现队列

力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

思路:两个栈,一个负责push数据,一个负责pop数据,在pop数据时,判断pop栈中是否有数据,没有则将push栈的数据全部导入,此时数据顺序反过来后再pop栈里出栈就可以实现先进先出这一特性了。

导入实现好的栈:


#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>typedef int STDatatype;
typedef struct Stack
{STDatatype* a;int capacity;int top;
}ST;void StackInit(ST* ps);
void StackDestroy(ST* ps);
void StackPush(ST* ps, STDatatype x);
void StackPop(ST* ps);//出栈
STDatatype StackTop(ST* ps);bool StackEmpty(ST* ps);
int StackSize(ST* ps);void StackInit(ST* ps)
{/*ps->a = NULL;ps->top = 0;ps->capacity = 0;*///初始化空间ps->a = (STDatatype*)malloc(sizeof(STDatatype) * 4);if (ps->a == NULL){perror("malloc");exit(-1);}ps->top = 0;ps->capacity = 4;
}static void check_capacity(ST* ps)
{if (ps->top == ps->capacity){int newCapacity = ps->capacity * 2;STDatatype* tmp = (STDatatype*)realloc(ps->a, newCapacity *sizeof(STDatatype));if (tmp == NULL){perror("realloc");exit(-1);}else{ps->a = tmp;ps->capacity == newCapacity;}}
}
void StackPush(ST* ps, STDatatype x)
{assert(ps);check_capacity(ps);ps->a[ps->top] = x;ps->top++;//指向下一个
}
void StackPop(ST* ps)//出栈
{assert(ps);//if(ps->top>0)assert(!StackEmpty(ps));ps->top--;
}STDatatype StackTop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));return ps->a[ps->top-1];//top为最后一个元素的下一个
}
void StackDestroy(ST* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->top = ps->capacity = 0;
}
bool StackEmpty(ST* ps)//判空
{assert(ps);return ps->top == 0;
}
int StackSize(ST* ps)
{assert(ps);return ps->top;
}

两个栈

typedef struct {ST pushst;//入队ST popst;//出队
} MyQueue;

Create


MyQueue* myQueueCreate() {//初始化+开空间MyQueue* st = (MyQueue*)malloc(sizeof(MyQueue));StackInit(&st->pushst);StackInit(&st->popst);return st;
}

Push

void myQueuePush(MyQueue* obj, int x) {assert(obj);StackPush(&obj->pushst,x);
}

判空 

bool myQueueEmpty(MyQueue* obj) {assert(obj);return StackEmpty(&obj->pushst) && StackEmpty(&obj->popst);
}

查看队头

由于popst的栈顶为队头,如果该栈为,需要导入pushst的数据。

int myQueuePeek(MyQueue* obj) {assert(obj);assert(!myQueueEmpty(obj)); //双栈判空if(StackEmpty(&obj->popst)){while(!StackEmpty(&obj->pushst))//导数据{StackPush(&obj->popst,StackTop(&obj->pushst));StackPop(&obj->pushst);}}return StackTop(&obj->popst);
}

Pop

同样涉及判空和调用队头元素,通过调用peek函数判断并完成对数据的导入并接收其返回值,

int myQueuePop(MyQueue* obj) {int qhead = myQueuePeek(obj);StackPop(&obj->popst);//出队return qhead;
}

释放

void myQueueFree(MyQueue* obj) {assert(obj);StackDestroy(&obj->pushst);StackDestroy(&obj->popst);free(obj);
}

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

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

相关文章

基于SpringBoot+MyBatis-Plus的教务管理系统

基于SpringBootMyBatis-Plus的教务管理系统 教务管理系统开发技术功能模块代码结构数据库设计运行截图源码获取 教务管理系统 欢迎访问此博客&#xff0c;是否为自己的毕业设计而担忧呢&#xff1f;是否感觉自己的时间不够做毕业设计呢&#xff1f;那你不妨看一下下面的文章&a…

openfeign整合sentinel出现异常

版本兼容的解决办法&#xff1a;在为userClient注入feign的接口类型时&#xff0c;添加Lazy注解。 Lazy注解是Spring Framework中的一个注解&#xff0c;它通常用于标记Bean的延迟初始化。当一个Bean被标记为Lazy时&#xff0c;Spring容器在启动时不会立即初始化这个Bean&…

腾讯云服务器收费标准是多少?腾讯云服务器收费标准表

你是否曾被繁琐复杂的服务器租赁费用搞得头昏脑胀&#xff1f;看着一堆参数和计费方式却毫无头绪&#xff1f;别担心&#xff0c;这篇文章就来帮你解决这个问题&#xff01;我们今天就来揭秘一下腾讯云服务器的收费标准&#xff0c;让大家轻松明白地知道如何租用腾讯云服务器。…

STM32定时器实现毫秒/秒级任务框架

STM32定时器实现毫秒/秒级任务框架 CubeMX配置代码分享总结 这是一期代码思路分&#xff0c;通过定时器&#xff08;以定时器10为例&#xff09;实现规定时间间隔执行指定任务。。。。。。 CubeMX配置 关于定时器的配置&#xff0c;这里不做介绍&#xff0c;不懂的可以看&#…

Stable Diffusion - StableDiffusion WebUI 软件升级与扩展兼容

欢迎关注我的CSDN&#xff1a;https://spike.blog.csdn.net/ 本文地址&#xff1a;https://spike.blog.csdn.net/article/details/134463035 目前&#xff0c;StableDiffusion WebUI 的版本是 1.6.0&#xff0c;同步更新 controlnet、tagcomplete、roop、easy-prompt-selector等…

合璧之光,共创辉煌|明道云伙伴大会2023圆满结束

2023年11月3日至11月4日&#xff0c;“合璧之光明道云伙伴大会2023”在上海星河湾酒店顺利举行&#xff0c;报名参会人数超过1800人。大会邀请到明道云标杆客户及合作伙伴分享组织落地零代码的经验及各行业领域解决方案&#xff0c;包括越秀集团、豫园股份、远大医药&#xff0…

君正X2100 读取CHIP_ID

每个处理器会有一个唯一的ID&#xff0c;这个ID可用做产品序列号&#xff0c;或其它。 X21000的CHIP_ID存放于芯片内部的efuse中&#xff0c;efuse是一次性可可编程存储器&#xff0c;初始值为全0&#xff0c;只能将0改为1&#xff0c;不能将1改为0。芯片出厂前会被写入一些信…

直击虎牙Q3财报:转身出击,一场应对不确定性的新战事

世界上唯一不变的&#xff0c;就是变化。 今年上半年&#xff0c;中国游戏市场销售仍在同比下降&#xff0c;游戏版号和行业政策都在趋严&#xff0c;游戏直播行业仍有较多不确定因素。延续过去的老路子&#xff0c;挖掘新的增量空间有限。 不过也要看到&#xff0c;行业出现…

Redis数据库双写一致性解决方案

程序员的公众号&#xff1a;源1024&#xff0c;获取更多资料&#xff0c;无加密无套路&#xff01; 最近整理了一份大厂面试资料《史上最全大厂面试题》&#xff0c;Springboot、微服务、算法、数据结构、Zookeeper、Mybatis、Dubbo、linux、Kafka、Elasticsearch、数据库等等 …

解决requests 2.28.x版本SSL错误:证书验证失败

1、问题背景 在使用requests 2.28.1版本时&#xff0c;我进行HTTP post传输报告负载时&#xff0c;由于SSL验证设置为True&#xff0c;请求失败&#xff0c;错误如下&#xff1a;(Caused by SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAILED] certifi…

ROC 曲线:健康背景下的应用和解释

一、介绍 在医疗保健领域&#xff0c;做出明智的决策对于改善患者治疗结果、有效分配资源和设计有效的诊断测试至关重要。受试者工作特征 (ROC) 曲线是一个强大的工具&#xff0c;在评估诊断测试的性能、区分健康个体和患病个体以及优化医疗保健干预方面发挥着至关重要的作用。…

[C++历练之路]vector的介绍以及底层模拟实现

W...Y的主页 &#x1f60a; 代码仓库分享 &#x1f495; &#x1f354;前言&#xff1a; 我们学习了STL中的string以及其所有重要接口并进行了模拟实现&#xff0c;但是STL中包含的内容不止于此。学习了string之后继续学习STL中的vector&#xff0c;学习成本会大大降低&#…