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. 带命名空间的XML的dom4j应用<转>

    Element root = document.getRootElement();     List   recordenvlist = document.selectNodes("//gm ...

  2. 简单文件系统构建ramdisk

    1.  BusyBox编译工具,包含bin, sbin, usr,  linuxrc. 2.  添加相关重要目录:dev,  etc,  mnt, proc,  sys, lib, var, tmp. ...

  3. Nginx_lua缓存问题,关闭lua_code_cache

    打开nginx.conf配置server{ lua_code_cache off; //关闭lua缓存 重启后生效 server_name localhost; default_type 'text/ ...

  4. [Django学习]上传图片

    上传图片 当Django在处理文件上传的时候,文件数据被保存在request.FILES FILES中的每个键为<input type="file" name="& ...

  5. eclipse新建python项Project interpreter not specified

    安装好pydev后新建python项目时提示”Project interpreter not specified“的错误,这是因为没有导入python开发环境所致 解决方法如下:1.找到eclipse ...

  6. 将DataFrame数据如何写入到Hive表中

    1.将DataFrame数据如何写入到Hive表中?2.通过那个API实现创建spark临时表?3.如何将DataFrame数据写入hive指定数据表的分区中? 从spark1.2 到spark1.3 ...

  7. .NET操作Excel笔记

    如果你新建一个项目的话,首先要添加Microsoft.Office.Core 与Microsoft.Office.Interop.Exce这两个应用,然后就能很方便的操作了,示例代码(只实现了简单的读 ...

  8. Error -27740: WSA_IO_pending

    Error -27740: WSA_IO_pendingAction.c(198): Error -27791: Server **** has shut down the connection pr ...

  9. 【转】【Asp.Net】ASP.Net Response.ContentType 详细列表

    不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 text/html 也就是网页格式. 代码如: <% response.ContentType =&quo ...

  10. android hardware.c 源码分析

    android的jni通过ID来找hal模块,使用了hw_get_module()函数,本文就通过这个函数的来分析一下hal层的模块是如何匹配的. 首先要了解三个结构体hw_module_t,hw_m ...