当前位置:网站首页>leetcode——错误的集合
leetcode——错误的集合
2022-06-24 07:07:00 【编程SHARE】
错误的集合

解题思路
建一个有序的集合,数据从小到大1~n。然后依次遍历原集合,原集合里面的数据出现一次就把有序集合中的该数据制成0,当遍历的时候有序集合出现为0的情况,就找到了重复的一个数值,当遍历完的时候,有序集合中非0的就是缺少的数值。
时间复杂度O(n),空间复杂度O(n)
代码
int* findErrorNums(int* nums, int numsSize, int* returnSize){
*returnSize=2;
int* duibi=(int*)malloc(sizeof(int)*numsSize);
int* ret=(int*)malloc((*returnSize)*sizeof(int));
int i=0;
for(i=0;i<numsSize;i++)
duibi[i]=i+1;
for(i=0;i<numsSize;i++)
{
if(duibi[nums[i]-1] ==0)
ret[0]=nums[i];
else
duibi[nums[i]-1]=0;
}
for(i=0;i<numsSize;i++)
if(duibi[i]!=0)
ret[1]=duibi[i];
free(duibi);
return ret;
}
边栏推荐
- There was an error checking the latest version of pip
- 1528. 重新排列字符串
- 【NOI模拟赛】摆(线性代数,杜教筛)
- K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream
- 【量化投资】离散傅里叶变换求数组周期
- 基于单片机开发的酒精浓度测试仪方案
- 偶然间得到的framework工具类 自用
- “不平凡的代理初始值设定不受支持”,出现的原因及解决方法
- Mysql数据(Liunx环境)定时备份
- Solving linear equations with MATLAB ax=b
猜你喜欢

input的聚焦后的边框问题

原生小程序用画布制作海报,等比例缩放,和uniapp差不多就是写法有点不同

Why can ping fail while traceroute can

什么是图神经网络?图神经网络有什么用?

Become an IEEE student member

数据中台:数据采集和抽取的技术栈详解

从华为WeAutomate数字机器人论坛,看政企领域的“政务新智理”

K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream

数据中台:数据中台技术架构详解

Mysql数据(Liunx环境)定时备份
随机推荐
Floating error waiting for changelog lock
Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
DataX User Guide
opencv最大值滤波(不局限于图像)
解决:模型训练时loss出现nan
表单图片上传在Chorme中无法查看请求体的二进制图片信息
2021-05-20computed和watch应用与区别
利用ngrok做内网穿透
MySQL | 存储《康师傅MySQL从入门到高级》笔记
Background management of uniapp hot update
It is enough to read this article about ETL. Three minutes will let you understand what ETL is
【团队管理】测试团队绩效管理的25点小建议
mysql写的代码数据 增删查改等等
rsync做文件备份
工具类
Xiaohei ai4code code baseline nibble 1
2138. 将字符串拆分为若干长度为 k 的组
Introduction to data platform
Telnet port login method with user name for liunx server
开源之夏中选名单已公示,基础软件领域成为今年的热门申请