给出一个包含 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实现:

方法一:

  1. class Solution {
  2. public int missingNumber(int[] nums) {
  3. int res=nums.length;
  4. int i=0;
  5. for(int num:nums){
  6. res^=num;
  7. res^=i;
  8. ++i;
  9. }
  10. return res;
  11. }
  12. }

方法二:

  1. class Solution {
  2. public int missingNumber(int[] nums) {
  3. int n=nums.length;
  4. int sum=0;
  5. for(int num:nums){
  6. sum+=num;
  7. }
  8. return (int)(0.5*n*(n+1)-sum);
  9. }
  10. }

方法三:

用二分查找法算出中间元素的下标,然后用元素值和下标值之间做对比,如果元素值大于下标值,则说明缺失的数字在左边,此时将r赋为m,反之则将l赋为m+1。排序的时间复杂度都不止O(n),但是在面试的时候,有可能数组就是排好序的,那么此时用二分查找法肯定要优于上面两种方法。

  1. class Solution {
  2. public int missingNumber(int[] nums) {
  3. Arrays.sort(nums);
  4. int l=0;
  5. int r=nums.length;
  6. while(l<r){
  7. int m=(l+r)>>1;
  8. if(nums[m]>m){
  9. r=m;
  10. }else{
  11. l=m+1;
  12. }
  13. }
  14. return r;
  15. }
  16. }

参考:https://www.cnblogs.com/grandyang/p/4756677.html

268 Missing Number 缺失的数字的更多相关文章

  1. [LeetCode] 268. Missing Number 缺失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  2. [LeetCode] 268. Missing Number ☆(丢失的数字)

    转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...

  3. 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 ...

  4. &lt;LeetCode OJ&gt; 268. Missing Number

    268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...

  5. [LeetCode] Missing Number 丢失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  6. 【LeetCode】268. Missing Number

    Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...

  7. 268. Missing Number@python

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  8. LeetCode 268. Missing Number (缺失的数字)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  9. 268. Missing Number序列中遗失的数字

    [抄题]: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...

随机推荐

  1. java.util.Scanner

    java.util.Scanner是Java5的新特征,主要功能是简化文本扫描.这个类最实用的地方表现在获取控制台输入,其他的功能都很鸡肋,尽管Java API文档中列举了大量的API方法,但是都不怎 ...

  2. 域名添加HTTPS

    准备 需要python版本为2.7以上,所以centos6需要把2.6升级成2.7 升级python ###安装python2.7 tar -xvf Python-2.7.5tar.bz2 cd Py ...

  3. Python学习系列之面向对象

    概述 一.Python编程方式 面向过程编程:根据业务逻辑从上到下磊代码 面向函数编程:将某功能代码封装到函数中,将来直接调用即可,无需重新写 面向对象编程:对函数进行分类.封装 二.面向过程编程 w ...

  4. Markdown 语法的简要规则

    标题 标题是每篇文章都须要也是最经常使用的格式,在 Markdown 中.假设一段文字被定义为标题,仅仅要在这段文字前加 # 号就可以. # 一级标题 ## 二级标题 ### 三级标题 以此类推,总共 ...

  5. Python循环定时服务功能(相似contrab)

    Python实现的循环定时服务功能.类似于Linux下的contrab功能.主要通过定时器实现. 注:Python中的threading.timer是基于线程实现的.每次定时事件产生时.回调完响应函数 ...

  6. 协方差矩阵与主成分分析PCA

    今天看论文,作者是用主成分分析(PCA)的方法做的.仔细学习了一下,有一篇博客写的很好,介绍的深入浅出! 协方差:http://pinkyjie.com/2010/08/31/covariance/ ...

  7. 编译自己的gcc

    1 编译gcc需要的依赖 gmp mpfr mpc isl binutils 将它们都安装在同一个目录下即可. 2 --disable-nls 将native language support关掉,只 ...

  8. Keys.BACKSPACE Keys.SPACE

    browser.find_element_by_xpath(xp_newpage).send_keys(Keys.SPACE)browser.find_element_by_xpath(xp_newp ...

  9. MPMoviePlayerController属性方法简介

    属性 说明 @property (nonatomic, copy) NSURL *contentURL 播放媒体URL,这个URL可以是本地路径,也可以是网络路径 @property (nonatom ...

  10. ThinkAndroid框架

    ThinkAndroid简介 ThinkAndroid是一个免费的开源的.简易的.遵循Apache2开源协议发布的Android开发框架,其开发宗旨是简单.快速的进行 Android应用程序的开发,包 ...