[LeetCode] 268. Missing Number 缺失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n
, find the one that is missing from the array.
Example 1
Input: [3,0,1]
Output: 2
Example 2
Input: [9,6,4,2,3,5,7,0,1]
Output: 8
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
给n个数,0到n之间有一个数字缺失了,让找到这个缺失数字。要求线性时间复杂度和常数级的额外空间复杂度。
解法1:排序,然后二分查找。此题数组不是排序的,所以适合。但面试时,如果问道要知道。
解法2:求差值,用0~n个数的和减去给定数组数字的和,差值就是要找的数字。
解法3:位操作Bit Manipulation,将这个数组与0~n之间完整的数异或一下累加到res,相同的数字异或后都变为了0,最后剩下的结果就是缺失的数字。
Java:
class Solution {
public int missingNumber(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int result = 0; Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if (result != nums[i]) {
return result;
}
result++;
}
return nums.length;
}
}
Java:
class Solution {
public int missingNumber(int[] nums) {
int xor = 0, i = 0; for (i = 0; i < nums.length; i++) {
xor = xor ^ i ^ nums[i];
} return xor ^ i;
}
}
Python:
def missingNumber(self, nums):
n = len(nums)
return n * (n+1) / 2 - sum(nums)
Python:
class Solution(object):
def missingNumber(self, nums):
return sum(xrange(len(nums)+1)) - sum(nums)
Python:
class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return reduce(operator.xor, nums, \
reduce(operator.xor, xrange(len(nums) + 1)))
C++: 用等差数列公式
class Solution {
public:
int missingNumber(vector<int>& nums) {
int sum = 0, n = nums.size();
for (auto &a : nums) {
sum += a;
}
return 0.5 * n * (n + 1) - sum;
}
};
C++:
class Solution {
public:
int missingNumber(vector<int>& nums) {
int res = 0;
for (int i = 0; i < nums.size(); ++i) {
res ^= (i + 1) ^ nums[i];
}
return res;
}
};
类似题目:
[CareerCup] 5.7 Find Missing Integer 查找丢失的数
All LeetCode Questions List 题目汇总
[LeetCode] 268. Missing Number 缺失的数字的更多相关文章
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- 268 Missing Number 缺失的数字
给出一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数.案例 1输入: [3,0,1]输出: 2案例 2输入: [9,6,4,2,3,5,7, ...
- LeetCode 268. Missing Number缺失数字 (C++/Java)
题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...
- LeetCode 268. Missing Number (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- Java [Leetcode 268]Missing Number
题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告
1.题目大意 Given an array nums, write a function to move all 0's to the end of it while maintaining the ...
- 33. leetcode 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- leetcode 268 Missing Number(异或运算的应用)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- Leetcode 268 Missing Number 位运算
题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组, ...
随机推荐
- opencv图片压缩视频并读取
import os import cv2 import numpy as np import time path = './new_image/' filelist = os.listdir(path ...
- 类型擦除对Java调用Kotlin的影响
@JvmName: 扩展方法相关: 先来定义一个扩展方法: 好,接下来再来定义一个扩展函数: 此时报错了..看一下错误提示: 其中给的提示有点奇怪,第一个是很明显咱们的扩展函数木有接收参数嘛,为啥提示 ...
- js插件---videojs中文文档详解
js插件---videojs中文文档详解 一.总结 一句话总结: js插件网上都有很多参考资料,使用起来也非常简单 二.lavarel中使用实例 <video id="example_ ...
- Linux UART介绍
1. UART介绍 UART是一类tty设备, 是一种串行端口终端, 具体可参考<UART接口介绍>在Linux中UART属于tty驱动的一部分, 具体实现包括驱动抽象层和硬件实现层 本文 ...
- nginx安装记录
1.下载nginx http://nginx.org/en/download.html 下载稳定版本,以nginx/Windows-1.12.2为例,直接下载 nginx-1.12.2 ...
- Python中实现count(distinct )
假设一个表有6个字段c1,c2,c3,c4,c5,c6,有如下的sql语句: select c1,count(distinct(c6)) from tbl where c3>1 group by ...
- sort()函数中的key
d = { , , } #for k in d.items(): # print(k) content = list(d.items()) print(content) content.sort(ke ...
- 2019.11.30 Mysql查询知识
不等于:<> 判断为空的条件:null和空格(空字符串) 判断是否为null:xxxx is not null / xxxx is null 判断null: SE ...
- win7虚拟机安装
https://blog.csdn.net/qq_16503045/article/details/81904986 iso下载地址 https://msdn.itellyou.cn/
- 使用vault pki engine 方便的管理证书
vault 是一个很方便的secret .敏感数据管理工具,当前的版本已经包含了UI,使用起来很方便 以下演示一个简单的pki 管理 项目使用docker-compose 运行,为了简单使用单机开发模 ...