当前位置:网站首页>leetcode-268.丢失的数字
leetcode-268.丢失的数字
2022-08-03 15:53:00 【KGundam】
位运算
题目详情
给定一个包含 [0, n] 中 n 个数的数组 nums ,找出 [0, n] 这个范围内没有出现在数组中的那个数。
示例1:
输入:nums = [3,0,1]
输出:2
解释:n = 3,因为有 3 个数字,所以所有的数字都在范围 [0,3] 内。2 是丢失的数字,因为它没有出现在 nums 中。
示例2:
输入:nums = [0,1]
输出:2
解释:n = 2,因为有 2 个数字,所以所有的数字都在范围 [0,2] 内。2 是丢失的数字,因为它没有出现在 nums 中。
示例3:
输入:nums = [9,6,4,2,3,5,7,0,1]
输出:8
解释:n = 9,因为有 9 个数字,所以所有的数字都在范围 [0,9] 内。8 是丢失的数字,因为它没有出现在 nums 中。
示例4:
输入:nums = [0]
输出:1
解释:n = 1,因为有 1 个数字,所以所有的数字都在范围 [0,1] 内。1 是丢失的数字,因为它没有出现在 nums 中。
第一种方法:排序比较元素和下标
我们可以将数组排序,存在的数字,将会与它的下标相对应
数组长n,下标为[0,n-1],假设缺失的数字为k,那么存在下面两种情况:
<1> 0≤k<n : 此时缺失的元素前面的元素和下标都一一对应,到了k变为nums[k] == k+1
<2> k == n : 此时0~n-1没有缺失的,所以对于任意nums[i]都是nums[i] == i,即元素和下标一一对应
代码如下:
class Solution
{
public:
int missingNumber(vector<int>& nums)
{
sort(nums.begin(), nums.end()); //排序
int n = nums.size();
for (int i = 0; i < n; ++i)
{
if (nums[i] != i) //第一种情况
return i;
}
return n; //第二种情况
}
};
第二种方法:哈希集合
本质上是和第一种方法一样的,只是降低了复杂度
首先遍历数组 nums,将数组中的每个元素加入哈希集合,然后依次检查从 0 到 n 的每个整数是否在哈希集合中,不在哈希集合中的数字即为丢失的数字。
代码如下:
class Solution
{
public:
int missingNumber(vector<int>& nums)
{
unordered_set<int> set;
int n = nums.size();
for (int i = 0; i < n; ++i) //将存在的元素都存入集合
set.insert(nums[i]);
int missing = -1; //因为0也是元素,所以初始化为-1
for (int i = 0; i <= n; ++i) //检查0~n的元素
{
if (!set.count(i)) //看哪个元素不在set中
{
missing = i; //缺失的就是这个元素
break;
}
}
return missing;
}
};
第三种方法:位运算!
这种方法的原理和leetcode-136.只出现一次的数字一样,可以点进去看一下
核心思想就是:一个数和它自己异或运算就会抵消掉归零
我们只需将所有元素和下标进行异或运算,最后留下来的即为找不到元素的下标
class Solution
{
public:
int missingNumber(vector<int>& nums)
{
int res = 0, n = nums.size();
for (int i = 0; i < n; ++i) //异或元素
res ^= nums[i];
for (int i = 0; i <= n; ++i) //异或下标
res ^= i;
/*也可以简写为: for (int i = 0; i < n; ++i) { res ^= nums[i]; res ^= i; } 这种方法需要注意的点是需要注意循环i取不到n 所以res要初始化为n */
return res;
}
};
第四种方法:数学方法

class Solution
{
public:
int missingNumber(vector<int>& nums)
{
int n = nums.size(), total = n * (n + 1) / 2, arrSum = 0;
for (int i = 0; i < n; ++i)
arrSum +=nums[i];
return total - arrSum;
}
};
位运算常用技巧

边栏推荐
- DC-DC 2C (40W/30W) JD6606SX2 power back application
- AWS China SDN Connector
- JS handwritten call apply bind (detailed) (interview)
- Go Go 简单的很,标准库之 fmt 包的一键入门
- Ark server opening tutorial win
- STM32的HAL和LL库区别和性能对比
- 不可忽略!户外LED显示屏的特点及优势
- 一文看懂推荐系统:召回03:基于用户的协同过滤(UserCF),要计算用户之间的相似度
- 泰山OFFICE技术讲座:段落边框的绘制难点在哪里?
- A new round of competition for speech recognition has started. Will natural dialogue be the next commanding height?
猜你喜欢

开源一夏 | 阿里云物联网平台之极速体验

一文看懂推荐系统:召回02:Swing 模型,和itemCF很相似,区别在于计算相似度的方法不一样

Internship Road: Documenting Confusion in My First Internship Project

AI+BI+可视化,Sugar BI架构深度剖析

出海季,互联网出海锦囊之本地化
![[Code Hoof Set Novice Village 600 Questions] Define a function as a macro](/img/7c/7e1469090ca3d1dea703b3fcee7428.png)
[Code Hoof Set Novice Village 600 Questions] Define a function as a macro

【Unity入门计划】基本概念(7)-Input Manager&Input类

简介undo log、truncate、以及undo log如何帮你回滚事物?

spark入门学习-1

With a single operation, I improved the SQL execution efficiency by 10,000,000 times!
随机推荐
5v充8.4v1A电流充电管理ic
leetcode:899. 有序队列【思维题】
使用Make/CMake编译ARM裸机程序(基于HT32F52352 Cortex-M0+)
MATLAB gcf图窗保存图像,黑色背景/透明背景
聊聊这个SaaS领域爆火的话题
AWS China SDN Connector
生态剧变,电子签名SaaS模式迎来新突破,网络效应加速到来
劲爆!协程终于来了!线程即将是过去式
CopyOnWriteArrayList详解
C#.NET 国密数字信封
Small Tools(4) 整合Seata1.5.2分布式事务
16 【过渡 动画】
How to get the 2 d space prior to ViT?UMA & Hong Kong institute of technology & ali SP - ViT, study for visual Transformer 2 d space prior knowledge!.
JD6606SP5_JD6606SSP_JD6606SASP_JD6621W7百盛新纪元授权代理商
Some optional strategies and usage scenarios for PWA application Service Worker caching
Ruoyi Ruoyi framework @DataScope annotation use and some problems encountered
Essentially a database data recovery 】 【 database cannot read data recovery case
不可忽略!户外LED显示屏的特点及优势
托尔斯泰:生活中只有两种不幸
Ark server open tool, server tutorial win