转载:http://www.cnblogs.com/grandyang/p/4756677.html

描述

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

For example,
Given nums = [0, 1, 3] return 2.

Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。

说明:

你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?

解析、代码

求和法

这道题给我们n个数字,是0到n之间的数但是有一个数字去掉了,让我们寻找这个数字,要求线性的时间复杂度和常数级的空间复杂度。那么最直观的一个方法是用等差数列的求和公式求出0到n之间所有的数字之和,然后再遍历数组算出给定数字的累积和,然后做减法,差值就是丢失的那个数字,参见代码如下:

解法一:

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;
}
};

异或法

这题还有一种解法,使用位操作Bit Manipulation来解的,用到了异或操作的特性,相似的题目有Single Number 单独的数字, Single Number II 单独的数字之二Single Number III 单独的数字之三。那么思路是既然0到n之间少了一个数,我们将这个少了一个数的数组合0到n之间完整的数组异或一下,那么相同的数字都变为0了,剩下的就是少了的那个数字了,参加代码如下:

nums[index] = index

解法二:

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;
}
};
public int missingNumber(int[] nums) {
int res = nums.length;
for (int i = 0; i < nums.length; ++i) {
res ^= (i) ^ nums[i];
}
return res;
}

二分法

这道题还可以用二分查找法来做,我们首先要对数组排序,然后我们用二分查找法算出中间元素的下标,然后用元素值和下标值之间做对比,如果元素值大于下标值,则说明缺失的数字在左边,此时将right赋为mid,反之则将left赋为mid+1。那么看到这里,作为读者的你可能会提出,排序的时间复杂度都不止O(n),何必要多此一举用二分查找,还不如用上面两种方法呢。对,你说的没错,但是在面试的时候,有可能人家给你的数组就是排好序的,那么此时用二分查找法肯定要优于上面两种方法,所以这种方法最好也要掌握以下~

解法三:

class Solution {
public:
int missingNumber(vector<int>& nums) {
sort(nums.begin(), nums.end());
int left = 0, right = nums.size();
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > mid) right = mid;
else left = mid + 1;
}
return right;
}
};

在CareerCup中有一道类似的题,5.7 Find Missing Integer 查找丢失的数,但是那道题不让我们直接访问整个int数字,而是只能访问其二进制表示数中的某一位,强行让我们使用位操作Bit Manipulation来解题,也是蛮有意思的一道题。

[LeetCode] 268. Missing Number ☆(丢失的数字)的更多相关文章

  1. [LeetCode] 268. Missing Number 缺失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  2. [LeetCode] Missing Number 丢失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  3. LeetCode 268. Missing Number (缺失的数字)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  4. 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 ...

  5. Java [Leetcode 268]Missing Number

    题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...

  6. 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 ...

  7. 268 Missing Number 缺失的数字

    给出一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数.案例 1输入: [3,0,1]输出: 2案例 2输入: [9,6,4,2,3,5,7, ...

  8. 33. leetcode 268. Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  9. leetcode 268 Missing Number(异或运算的应用)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

随机推荐

  1. 20145304 网络对抗技术 逆向与Bof基础

    20145304 网络对抗技术 逆向与Bof基础 实践目标 学习以下两种方法,运行正常情况下不会被运行的代码: 手工修改可执行文件,改变程序执行流程,直接跳转到getShell函数. 利用foo函数的 ...

  2. 20145305 《网络对抗》Web安全基础实践

    实践过程及结果截图 Phishing with XSS 在文本框里面写一个钓鱼网站代码就可以了 </form> <script> function hack(){ XSSIma ...

  3. 20165310_获奖感想与Java阶段性学习总结

    获奖感想与Java阶段性学习总结 一.Learning By Doing ​ 在此之前,其实我并没有想到能够成为小黄杉的第一批成员之一,喜悦之余,也感受到了许多的压力.小黄杉一方面代表了老师对于我这一 ...

  4. 2018-2019-1 20189218《Linux内核原理与分析》第九周作业

    进程调度的时机 进程调度时机就是内核调用schedule函数的时机.当内核即将返回用户空间时,内核会检查need_resched标志是否设置.如果设置,则调用schedule函数,此时是从中断(或者异 ...

  5. thinkphp中的__DIR__ __ROOT__ __APP__ __MODULE__ APP_PATH LIB_PATH MODULE_PATH 等是在哪里定义的?

    为什么会产生这样的 路径 常量等 的 困扰? 是由于 在tp中, 使用了多种形式的常量导致的, 比如, 有php语言本身的 "魔术常量", 有 php函数, 比如dirname定义 ...

  6. 51nod 1051 最大子矩阵和

    没想到居然可以O(n3)暴力过 就是大概之前的  最大连续子序列和 加成2维度了  枚举起始列 和 终止列 然后计算从1到n行最大的子矩阵的和 注意n 和 m 的输入顺序!! #include< ...

  7. .Net Core集成Office Web Apps(一)

    最近开始学习.Net Core,并使用Visual Studio Code工具来开发.感觉开发起来特别的方便,但是有个头疼的地方:许多的类库被修改了,一时半会儿还熟悉不了,需要查阅官方API... M ...

  8. C# asp.net 比较两个时间的差求天数

    string str1 = "2017-2-13 23:59:59"; string str2 = "2017-2-14 0:00:01"; DateTime ...

  9. hdu 4352 XHXJ's LIS 数位dp+状态压缩

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others ...

  10. redis教程(The little redis book中文版)

    许可证 <The Little Redis Book>是经由Attribution-NonCommercial 3.0 Unported license许可的,你不需要为此书付钱. 你可以 ...