当前位置:网站首页>leetcode 268. Missing Numbers (XOR!!)
leetcode 268. Missing Numbers (XOR!!)
2022-08-03 20:13:00 【Luna programming】
给定一个包含 [0, n] 中 n 个数的数组 nums ,找出 [0, n] 这个范围内没有出现在数组中的那个数.
示例 1:
输入:nums = [3,0,1]
输出:2
解释:n = 3,(n为数组中元素个数)因为有 3 个数字,所以所有的数字都在范围 [0,3] 内.2 是丢失的数字,因为它没有出现在 nums 中.
示例 2:
输入:nums = [9,6,4,2,3,5,7,0,1]
输出:8
解释:n = 9,因为有 9 个数字,所以所有的数字都在范围 [0,9] 内.8 是丢失的数字,因为它没有出现在 nums 中.
提示:
n == nums.length
1 <= n <= 104
0 <= nums[i] <= n
nums 中的所有数字都 独一无二
进阶:你能否实现线性时间复杂度、仅使用额外常数空间的算法解决此问题?
思路一:异或
首先将从0到nAll values of are XORed once,This is to record all the values,Then XOR each value in the array,Because the same number is XORed2times have no effect on the original value,即 a ^ b ^ b =a .
Then in the end, only the number that did not appear is left.
class Solution {
public:
int missingNumber(vector<int>& nums) {
int ans=0;
int n=nums.size();
for(int i=0;i<=n;++i)
ans^=i;
for(vector<int>::iterator it=nums.begin();it!=nums.end();++it)
ans^=(*it);
return ans;
}
};
思路二: 作差
先从0一直加到n,Record the sum when all numbers are present sum,Then add each value in the array he,他们的差值(sum-he)That is, the number that does not appear.
class Solution {
public:
int missingNumber(vector<int>& nums) {
int sum=0,he=0;
sort(nums.begin(),nums.end());
int n=nums.size();
for(int i=0;i<=n;++i)
sum+=i;
for(vector<int>::iterator it=nums.begin();it!=nums.end();++it)
he+=(*it);
return sum-he;
}
};
思路三:排序
对数组进行排序,If the serial number and value of the corresponding position are not equal,Then the ordinal is the missing number.
class Solution {
public:
int missingNumber(vector<int>& nums) {
sort(nums.begin(),nums.end());
int n=nums.size(),i=0;
for( ;i<n;++i){
if(i!=nums[i])
break;
}
return i; //If the last number is missing,Then the loop end conditioni==n,最后返回的也是n
}
};
边栏推荐
- Li Mu hands-on learning deep learning V2-BERT fine-tuning and code implementation
- 详解AST抽象语法树
- JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
- 622 设计循环队列——Leetcode天天刷【循环队列,数组模拟,双指针】(2022.8.2)
- leetcode 326. 3 的幂
- 单调栈及其应用
- 染料修饰核酸RNA|[email protected] 610/[email protected] 594/Alexa 56
- 不知道这4种缓存模式,敢说懂缓存吗?
- tensorflow-gpu2.4.1安装配置详细步骤
- 【STM32】标准库-自定义BootLoader
猜你喜欢

149. 直线上最多的点数-并查集做法

从腾讯阿里等大厂出来创业搞 Web3、元宇宙的人在搞什么

简易电子琴设计(c语言)

微导纳米IPO过会:年营收4.28亿 君联与高瓴是股东

Matlab paper illustration drawing template No. 42 - bubble matrix diagram (correlation coefficient matrix diagram)

小马智行起诉擎天智卡:索赔6000万 彭军称要斗争到底

不知道这4种缓存模式,敢说懂缓存吗?

转运RNA(tRNA)甲基化修饰7-甲基胞嘧啶(m7C)|tRNA-m7G

622 设计循环队列——Leetcode天天刷【循环队列,数组模拟,双指针】(2022.8.2)

Statistical machine learning 】 【 linear regression model
随机推荐
若依集成easyexcel实现excel表格增强
演讲议题及嘉宾重磅揭晓,TDengine 开发者大会推动数据技术“破局”
Leetcode sword refers to Offer 15. 1 in the binary number
Internet Download Manager简介及下载安装包,IDM序列号注册问题解决方法
leetcode 2119. Numbers reversed twice
leetcode 899. 有序队列
染料修饰核酸RNA|[email protected] 610/[email protected] 594/Alexa 56
WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件
Detailed steps for tensorflow-gpu2.4.1 installation and configuration
消除对特权账户的依赖使用Kaniko构建镜像
单调栈及其应用
leetcode 剑指 Offer 58 - II. 左旋转字符串
C51 存储类型与存储模式
Abs (), fabs () and LABS ()
【微信小程序2】事件传参与数据同步[03]
622 设计循环队列——Leetcode天天刷【循环队列,数组模拟,双指针】(2022.8.2)
ES6-箭头函数
头条服务端一面经典10道面试题解析
2022 年值得尝试的 7 个 MQTT 客户端工具
Detailed explanation of JWT