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 ...
随机推荐
- [Kernel]内核版本添加字符和内核版本'+'解决
转自:http://blog.csdn.net/adaptiver/article/details/7225980 之前每次由于git仓库编译出来每次都带有'+', 导致都需要使用git archiv ...
- uboot中fb实现
帧缓冲区fb在内存中,要实现fb同步显示需要设定DMA操作. 设定LCD的DMA操作,要在开始操作LCD之前. common/lcd.c中定义lcd_init() --> driver/vide ...
- WPF教程二:布局之StackPanel面板
应用程序界面设计中,合理的元素布局至关重要,它可以方便用户使用,并将信息清晰合理地展现给用户.WPF提供了一套功能强大的工具-面板(Panel),来控制用户界面的布局.你可以使用这些面板控件来排布元素 ...
- smo算法matlab实现
看完CSDN上结构之法,算法之道的支持向量机通俗导论(理解SVM的三层境界) http://blog.csdn.net/v_july_v/article/details/7624837 参考了 ...
- C++11 新特性:Lambda 表达式
参考文章:https://blogs.oracle.com/pcarlini/entry/c_1x_tidbits_lambda_expressions 或许,Lambda 表达式算得上是 C++ 1 ...
- 关于Cocos2d-x中数组的使用
1.定义和背景 cocos2d::Vector<T> 是一个封装了动态大小的数组的顺序型容器. 它的元素是连续存储的,cocos2d::Vector<T> 的存储是自动处理的. ...
- iOS多线程与网络开发之NSOperation
郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...
- 搜集的一些酷炫的金属色 ,RGB值 和大家分享一下
开发iOS程序过程中会使用到RGB,要注意每个RGB值都要除以 255.0 ,注意: ' .0 ' 不能省!! 一下是本人搜集的一些酷炫金属色的RGB值: 黄金 242,192,86 石墨 87, ...
- JavaStuNote 5
接口 (interface) 一个抽象类,全部的方法都是抽象的,全部方法的public, 我们把这种类叫做极度抽象类,是最干瘪的类. public abstract class A { public ...
- android SDK manager 快速更新【转】