268 Missing Number 缺失的数字
给出一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。
案例 1
输入: [3,0,1]
输出: 2
案例 2
输入: [9,6,4,2,3,5,7,0,1]
输出: 8
注意事项:
您的算法应该以线性复杂度运行。你能否仅使用恒定的额外空间复杂度来实现它?
详见:https://leetcode.com/problems/missing-number/description/
Java实现:
方法一:
class Solution {
public int missingNumber(int[] nums) {
int res=nums.length;
int i=0;
for(int num:nums){
res^=num;
res^=i;
++i;
}
return res;
}
}
方法二:
class Solution {
public int missingNumber(int[] nums) {
int n=nums.length;
int sum=0;
for(int num:nums){
sum+=num;
}
return (int)(0.5*n*(n+1)-sum);
}
}
方法三:
用二分查找法算出中间元素的下标,然后用元素值和下标值之间做对比,如果元素值大于下标值,则说明缺失的数字在左边,此时将r赋为m,反之则将l赋为m+1。排序的时间复杂度都不止O(n),但是在面试的时候,有可能数组就是排好序的,那么此时用二分查找法肯定要优于上面两种方法。
class Solution {
public int missingNumber(int[] nums) {
Arrays.sort(nums);
int l=0;
int r=nums.length;
while(l<r){
int m=(l+r)>>1;
if(nums[m]>m){
r=m;
}else{
l=m+1;
}
}
return r;
}
}
参考:https://www.cnblogs.com/grandyang/p/4756677.html
268 Missing Number 缺失的数字的更多相关文章
- [LeetCode] 268. Missing Number 缺失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- LeetCode 268. Missing Number缺失数字 (C++/Java)
题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
- [LeetCode] Missing Number 丢失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- 268. Missing Number@python
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- LeetCode 268. Missing Number (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 268. Missing Number序列中遗失的数字
[抄题]: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
随机推荐
- how to read openstack code
本文的目的不是介绍openstack.我们这里假设你已经知道了openstack是什么,能够做什么.所以目的是介绍如何阅读openstack的代码.通过读代码来进一步学习openstack. 转载要求 ...
- 如何使用python书写守护进程?daemon、python-daemon
可以参考的supervisor实现:https://github.com/Supervisor/supervisor:http://supervisord.org/configuration.html ...
- Open Flash Chart IO ERROR Loading test data Error #2032
http://blog.sina.com.cn/s/blog_6754464e0100qfvd.html Open Flash Chart 2 提示Open Flash Chart IO ERROR ...
- 离线安装Cloudera Manager5.3.4与CDH5.3.4
文章转载:http://www.aboutyun.com/thread-14024-1-1.html 前期准备工作(系统环境搭建) 操作系统:CentOS 6.5 x64 CPU*2 64G 300G ...
- NA路由①
Cisco设备的端口: 在Cisco的路由器上都有一个带外网管口(Console/AUX): Con口主要用于本地的con线进行本地网管: AUX口主要与Modem连接通过固话 ...
- HDU 5407 CRB and Candies(LCM +最大素因子求逆元)
[题目链接]pid=5407">click here~~ [题目大意]求LCM(Cn0,Cn1,Cn2....Cnn)%MOD 的值 [思路]来图更直观: 这个究竟是怎样推出的.说实话 ...
- 一个python自动化测试的例子
http://blog.csdn.net/galen2016/article/details/70882483 https://www.cnblogs.com/TankXiao/category/47 ...
- .net Core使用Orcle官方驱动连接数据库 C#参考教程 http://www.csref.cn
.net Core使用Orcle官方驱动连接数据库 最近在研究.net Core,因为公司的项目用到的都是Oracle数据库,所以简单试一下.net Core怎样连接Oracle. Oracle官 ...
- [Erlang危机](5.1.4)端口port
原创文章,转载请注明出处:server非业余研究http://blog.csdn.net/erlib 作者Sunface 或port drivers15. 全程跟踪端口数会对诊断负载或进程泄漏 ...
- Eclipse的安装使用
1.从官网下载最新的Eclipse http://www.eclipse.org/downloads/