LeetCode——Missing Number
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的更多相关文章
- [LeetCode] Missing Number 丢失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- (leetcode)Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [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 ...
- LeetCode Missing Number (简单题)
题意: 给一个含有n个整数的数组,数组中的元素应该是0-n.现在缺了其中某1个,找出缺少的那个整数? 思路: 0-n的总和是可以直接计算的,而缺少的那个就是sum减去数组的和. int missing ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- [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 ...
- Leetcode-268 Missing Number
#268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...
随机推荐
- Jackson2.1.4 序列化对象时对属性的过滤
//对field(所有字段)进行过滤 //对get方法进行过滤 //对isBoolean这样的方法进行过滤 //里面的具体配置有 ANY,DEFAULT,NON_PRIVATE,NONE,PROTEC ...
- Java-DBCP连接池
创建项目: 导入jar包: 参见上图. JDBCConn.java获取数据源类: package com.gordon.jdbcconn; import java.io.InputStream; im ...
- Kafka Tools
参考, https://cwiki.apache.org/confluence/display/KAFKA/System+Tools https://cwiki.apache.org/confluen ...
- Hadoop2的Yarn和MapReduce2相关
转自: http://www.aboutyun.com/thread-7678-1-1.html.. 问题导读: 1.什么是yarn? 2.Yarn 和MapReduce相比,它有什么特殊作用 ? ...
- hadoop job解决大数据量关联时数据倾斜的一种办法
转自:http://www.cnblogs.com/xuxm2007/archive/2011/09/01/2161929.html http://www.geminikwok.com/2011/04 ...
- 弄明白html、css3、js这个问题。。。
- new/delete 的使用要点
运算符 new 使用起来要比函数 malloc 简单得多,例如: int *p1 = (int *)malloc(sizeof(int) * length); int *p2 = new int[le ...
- jquery -- jquery控制只能输入数字和小数点
控制文本框只能输入数字是一个很常见的需求,比如电话号码的输入.数量的输入等,这时候就需要我们控制文本框只能输入数字.在用js控制之后在英文输入法的状态下去敲击键盘上的非数字键是输不进去的,然而当你转到 ...
- Graphviz 对网状结构进行可视化
Graphviz 是一款开源的,免费的图结构的可视化软件,只需要描述清楚图中的顶点,边的信息,Graphviz 可以自动化的对图进行布局,生成对应的图片: Graphviz 采用DOT 的这种语言来描 ...
- 资深投资人全力反击: VC增值平台从来就不是一坨狗屎
编者注: 本文来自海外著名科技博客VentureBeat, 英文原文出自Kyle Lacy之手 ,中文版由天地会珠海分舵进行编译. 文章主要是针对前几天德国VC Christian Claussen的 ...