【数组】Missing Number

题目:

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
};
时间: 2024-10-15 03:15:21

【数组】Missing Number的相关文章

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!. Note: Your solution should be in logarithmic time complexity. (Easy) 分析:求n的阶乘中末位0的个数,也就是求n!中因数5的个数(2比5多),简单思路是遍历一遍,对于每个数,以此除以5求其因数5的个数,但会超时. 考虑到一个数n比他小

&lt;LeetCode OJ&gt;Missing Number【268】

268. Missing Number My Submissions Question Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium 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 num

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

268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium 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.

LeetCode:Missing Number - 缺失的数字

1.题目名称 Missing Number (缺失的数字) 2.题目地址 https://leetcode.com/problems/missing-number 3.题目内容 英文:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n  find the one that is missing from the array. 中文:给出一个包含了n个不同数字的数组,从0开始一直到n,找出缺失的数字.如果数

[LintCode] Find the Missing Number 寻找丢失的数字

Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example Given N = 3 and the array [0, 1, 3], return 2. Challenge Do it in-place with O(1) extra memory and O(n) time. 这道题是LeetCode上的原题,请参见我之前的博客Missing Number

LeetCode 136. Single Number &amp; 268. Missing Number

136. Single Number 考察的是异或运算.相同的数异或结果为0,一个数与0异或还是原来的数,以及异或符合交换律.因此,把所有的数都异或起来,结果就是落单的那个数. class Solution { public: int singleNumber(vector<int>& nums) { int res=0; for (int num:nums){ res ^= num; } return res; } }; 268. Missing Number 可以用数学方法直接做,

leetcode Missing Number

题目连接 https://leetcode.com/problems/missing-number/ 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$. Not

hdu 5166 Missing number

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5166 Missing number Description There is a permutation without two numbers in it, and now you know what numbers the permutation has. Please find the two numbers it lose. Input There is a number T shows t

【LeetCode】268. Missing Number

Missing Number 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 i

HDU - 5166 - Missing number &amp;&amp; 5167 - Fibonacci

Missing number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 430    Accepted Submission(s): 233 Problem Description There is a permutation without two numbers in it, and now you know what num