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. Hive shell 命令

    Hive shell 命令. 连接 hive shell 直接输入 hive 1.显示表 hive> show tables; OK test Time taken: 0.17 seconds, ...

  2. 浏览器向下兼容之polyfill[阅后即瞎]

    我们入门JavaScript的时候都写过polyfill: 比如手写一个弹窗, 手动模拟实现一个表格, 这些魔力的对象都是浏览器原生支持的, 虽然当我成为JS专家之后再也没造过轮子, 但是最近才发现我 ...

  3. Java中Calendar.DAY_OF_WEEK需要减一的原因

    http://blog.sina.com.cn/s/blog_45c06e600100pm77.html ——————————————————————————————————————————————— ...

  4. 如何对抗、预防 SQL注入 攻击

    一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库. 二.SQL注入攻击的总体 ...

  5. css制作上下左右的箭头

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. write solid code 零散(原文)

    整理下目录,看了这个文件,幸好未删除. 以下是<write solid code>中的原文摘录. 1.How could I have prevented this bug? 2.How ...

  7. 【MongoDB】数组长度查询

    db.groupedInfo.count({'surveyInfo.surveyAndUserID.0':{$exists:1}})

  8. Ubuntu 12.04.2 安装 Oracle11gR2

    #step 1: groupadd -g 2000 dbauseradd -g 2000 -m -s /bin/bash -u 2000 griduseradd -g 2000 -m -s /bin/ ...

  9. 怎么让Word编辑公式又快又好

    现在很多办公学习都是在电脑中进行的.很多文件论文都是在Word中编写定稿以后再打印成册或者去投稿.毫无疑问,在Word中编辑各种各样的文字与符号是一项现在社会中非常必要的技能,而这其中一项就是对公式的 ...

  10. jQuery Datatable 转载

    jQuery Datatable 实用简单实例 时间 2014-05-08 10:44:18  51CTO推荐博文 原文  http://sgyyz.blog.51cto.com/5069360/14 ...