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 ...
随机推荐
- html5视频播放器 一 (改写默认样式)
一个项目用到了html5视频播放器,于是就写了一个,走了很多坑,例如在chrome中加载视频出现加载异常等 先看看效果 是不是感觉换不错,以下是我播放器改写样式的布局. <!DOCTYPE ht ...
- Ubuntu 16.04使用百度云的方案
Ubuntu没有很好的解决方案,都是一些投机取巧的方案: 1.不建议安装百度云客户端,尤其对于免费用户来说,会限制速度. 2.可以使用网页版进行文件上传. 3.下载可以通过Chrome点击下载后,复制 ...
- 立面图 平面图 剖面图 CAD
http://www.qinxue.com/88.html http://www.xsteach.com/course/2855 前后左右各个侧面的外部投影图——立面图:对建筑物各个侧面进行投影所得到 ...
- UIButton和UISlider
UIButton 主要功能:按钮控件,主要用于与用户操作进行交互 经常使用属性及方法 系统内建的按钮类型 UIButtonTypeCustom UIButtonTypeSystem UIButtonT ...
- 大话设计模式C++实现-第8章-工厂方法模式
一.UML图 二.概念 工厂方法模式(Factory Method):定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法是一个类的实例化延迟到其子类. 三.包括的角色 (1)抽象工厂 (2 ...
- symfony 数据库中文乱码
这个问题 是由于编辑器没有设置utf8格式造成的,当然config里也要设置utf8 解决方法:编辑器设置utf8,重启 doctrine: dbal: driver: pdo_mysql host: ...
- how to get geometry type of layer using IMapServer3 and IMapLayerInfo? (C#)
http://forums.arcgis.com/threads/11481-how-to-get-geometry-type-of-layer-using-IMapServer3-and-IMapL ...
- 从头认识Spring-2.3 注解装配-@autowired(4)-required(2)
这一章节我们来继续具体讨论一下@autowired里面的參数required.在多构造器注入的情况. 1.domain(重点) 蛋糕类: package com.raylee.my_new_sprin ...
- C中多线程开发
1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期.solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,可是在一个进程(proce ...
- Android小技巧总结1
0.Android彻底退出引用程序的方法 1) Dalvik VM的本地方法 android.os.Process.killProcess(android.os.Process.myPid()); / ...