当前位置:网站首页>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;
}
边栏推荐
- 110. 平衡二叉树-递归法
- There was an error checking the latest version of pip
- 常用表情符号
- Jenkins自动化部署,连接不到所依赖的服务【已解决】
- Fast and slow pointer series
- Increase insert speed
- 数据中台:中台架构及概述
- K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream
- [10 day SQL introduction] Day2
- 数据库迁移从PostgreSQL迁移到 MYSQL
猜你喜欢
rpiplay实现树莓派AirPlay投屏器
Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
Why can ping fail while traceroute can
OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集
【E325: ATTENTION】vim编辑时报错
China chip Unicorn Corporation
教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5
用VNC Viewer的方式远程连接无需显示屏的树莓派
【使用 PicGo+腾讯云对象存储COS 作为图床】
Mysql数据(Liunx环境)定时备份
随机推荐
IDEA另起一行快捷键
Telnet port login method with user name for liunx server
GradScaler MaxClipGradScaler
一文详解|增长那些事儿
1704. 判断字符串的两半是否相似
【牛客】HJ1 字符串最后一个单词的长度
SLAM14讲中Sophus包的安装问题
什么是图神经网络?图神经网络有什么用?
【牛客】把字符串转换成整数
【团队管理】测试团队绩效管理的25点小建议
Prompt code when MySQL inserts Chinese data due to character set problems: 1366
[force deduction 10 days SQL introduction] Day3
MySQL | 存储《康师傅MySQL从入门到高级》笔记
Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)
Scheduled database backup script
OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集
Liunx change the port number of vsftpd
Win11 blank when using VIM to view content in cmder
How does the tunnel mobile inspection track robot monitor 24 hours to ensure the safety of tunnel construction?
阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍