Description:

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?

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

查找0~n缺失的数。

首先可以先对数组排序,然后线性查找。时间复杂度是O(nlogn + n),不满足题意但是也可以AC。

public class Solution {
public int missingNumber(int[] nums) { Arrays.sort(nums); if(nums[0] != 0) {
return 0;
} for(int i=0; i<nums.length-1; i++) {
if(nums[i] + 1 != nums[i+1]) {
return nums[i] + 1;
}
} return nums[nums.length - 1] + 1;
}
}

如果要求是线性时间复杂度可以以空间换时间。设置一个辅助数组来记录存在的数。时间复杂度是O(n),空间复杂度是O(n)

public class Solution {
public int missingNumber(int[] nums) { int[] count = new int[nums.length + 1]; Arrays.fill(count, 0);
//0 0 for(int i=0; i<nums.length; i++) {
count[nums[i]] = 1;
} //0 1 for(int i=0; i<=nums.length; i++) {
if(count[i] == 0) {
return i;
}
} return 0;
}
}

以上两种方法并不完全满足题目要求,题目的最终要求是在线性时间复杂度和常数空间复杂度下完成。可以利用等差数列的求和公式求出0~n的和,然后逐一减去nums中的数剩下的也就是缺失的那个数了。时间复杂度O(n),空间复杂度O(1)。

public class Solution {
public int missingNumber(int[] nums) { int n = nums.length; int sum = (1 + n) * n / 2; for(int i=0; i<nums.length; i++) {
sum -= nums[i];
} return sum;
}
}

LeetCode——Missing Number的更多相关文章

  1. [LeetCode] 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] Missing Number (A New Questions Added Today)

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

  4. LeetCode Missing Number (简单题)

    题意: 给一个含有n个整数的数组,数组中的元素应该是0-n.现在缺了其中某1个,找出缺少的那个整数? 思路: 0-n的总和是可以直接计算的,而缺少的那个就是sum减去数组的和. int missing ...

  5. 【LeetCode】268. Missing Number

    Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...

  6. &lt;LeetCode OJ&gt; 268. Missing Number

    268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...

  7. [LeetCode] Single Number 单独的数字

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  8. [LintCode] Find the Missing Number 寻找丢失的数字

    Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example G ...

  9. Leetcode-268 Missing Number

    #268.  Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...

随机推荐

  1. (转)Linux下PS命令详解

    (转)Linux下PS命令详解 整理自:http://blog.chinaunix.net/space.php?uid=20564848&do=blog&id=74654 要对系统中进 ...

  2. ExtJs 通过分析源代码解决动态加载Controller的问题

    通过分析源代码解决动态加载Controller的问题 最近在研究ExtJs(4.2.0)的MVC开发模式,具体Extjs的MVC如何使用这里不解释,具体参见ExtJs的官方文档.这里要解决的问题是如何 ...

  3. ​网页图表Highcharts实践教程标之加入题副标题版权信息

    ​网页图表Highcharts实践教程标之加入题副标题版权信息 Highcharts辅助元素 辅助元素图表的非必要元素.如标题.版权信息.标签.加载动态.它们不和图表数据发生关联,仅仅是额外说明一些基 ...

  4. 数据导入报错 Got a packet bigger than‘max_allowed_packet’bytes

    数据导入报错:Got a packet bigger than‘max_allowed_packet’bytes的问题 2个解决方法: 1.临时修改:mysql>set global max_a ...

  5. 05 Oracle process

    本章提要----------------------------------------------所有的 process 都是在 PGA 内(memory)server process: 与 cli ...

  6. js学习笔记29----拖拽

    原理:先计算鼠标与拖拽目标的左侧距离 跟 上面距离,再计算拖动后的位置. 示例代码: <!DOCTYPE html> <html lang="en"> &l ...

  7. 获取表单提交MVC错误信息

    if (!ModelState.IsValid)                {                    List<string> Keys = ModelState.Ke ...

  8. Deep Learning 经典网路回顾#之LeNet、AlexNet、GoogLeNet、VGG、ResNet

    #Deep Learning回顾#之LeNet.AlexNet.GoogLeNet.VGG.ResNet 深入浅出——网络模型中Inception的作用与结构全解析 图像识别中的深度残差学习(Deep ...

  9. Spring 4 官方文档学习(十一)Web MVC 框架之locales

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-localeresolve ...

  10. jquery 中json数组的操作(转)

    在jquery中处理JSON数组的情况中遍历用到的比较多,但是用添加移除这些好像不是太多. 今天试过json[i].remove(),json.remove(i)之后都不行,看网页的DOM对象中好像J ...