题目:

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?

思路:

先将数组排序,然后进行二分搜索。显然,中点的下标和中点的值相同时,说明从起始到中点没有错位,缺失数应该在数组后边。如果不相等,说明前面已经有错位,缺失数在左边。如果缺失数是最后一个的话,那整个数组都没有错位,则要返回最后一个加1。

/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function(nums) {
if(nums.length==0){
return;
}
nums.sort(function(a,b){return a-b;});
var l=0,r=nums.length-1,middle=0;
while(l<r){
middle=l+Math.floor((r-l)/2);
if(middle!=nums[middle]){
r=middle-1;
}
if(middle==nums[middle]){
l=middle+1;
}
} return nums[l]==l?nums[l]+1:l
};

【数组】Missing Number的更多相关文章

  1. PAT 甲级 1144 The Missing Number (20 分)(简单,最后一个测试点没过由于开的数组没必要大于N)

    1144 The Missing Number (20 分)   Given N integers, you are supposed to find the smallest positive in ...

  2. Missing number

    Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...

  3. Missing Number, First Missing Positive

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

  4. [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 ...

  5. PAT 1144 The Missing Number[简单]

    1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...

  6. 一道面试题Lintcode196-Find the Missing Number

    http://www.lintcode.com/en/problem/find-the-missing-number/# Find the Missing Number Given an array ...

  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. &lt;LeetCode OJ&gt; 268. Missing Number

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

  9. LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

    数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...

  10. Leetcode-268 Missing Number

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

随机推荐

  1. python文件对比

    #-*- encoding:utf-8 -*- class loadDatas(object): def __init__(self): self.path='./data' def load_com ...

  2. STL中的Vector相关用法

    STL中的Vector相关用法 标准库vector类型使用需要的头文件:#include <vector>. vector 是一个类模板,不是一种数据类型,vector<int> ...

  3. Quartus II中使用脚本转换sof到rbf文件

    1.  新建一个文本文件,保存为任意但有意义的名字,如:sof_to_rbf.bat,注意,保存时请不要使用默认的格式,应该手动从.txt切换为all files 2.  在文本中输入以下内容: %Q ...

  4. hdu 2780 Su-Su-Sudoku(DFS数独)

    题目链接:hdu2780 #include<stdio.h> #include<string.h> #include<queue> #include<math ...

  5. Fig723.asy

    import settings; outformat="pdf"; tex="xelatex"; usepackage("amsmath") ...

  6. delphi 6数据库连接之长短模式(sqlserver)

    delphi 6数据库连接之长短模式(sqlserver) 标签: delphi数据库 2015-08-12 20:59 351人阅读 评论(0) 收藏 举报  分类: delphi(3)  版权声明 ...

  7. TSQL--按某字段列分组,在将各组中某列合并成一行

    鉴于群里很多同事在问这个问题,我简单写个Demo,希望对初学者有帮助! 无真相,无解说,不解释,直接上Code! --========================================= ...

  8. c# 控制台应用程序怎么隐藏黑窗口

    class Program     {         [DllImport("user32.dll", EntryPoint = "ShowWindow",  ...

  9. .net core Memcached使用

    首先,你要在你电脑上安装配置好Memcached环境哦 Startup类的ConfigureServices中加入 //memcachedcore1 services.AddEnyimMemcache ...

  10. shiro之深度解析FormAuthenticationFilter

      shiro是我们在项目经常使用到的权限管理框架,本文我们就重点来分析FormAuthenticationFilter的验证过程. FormAuthenticationFilter 1.继承结构   ...