leetcode笔记: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?

二. 题目分析

题目大意是,给定一个包含从0, 1, 2, ..., n, 选出的n个不同数字的数组,从中找出数组中缺失的那一个数。并给出一个简单的例子。

题目要求算法能满足线性时间复杂度和常数空间复杂度。

由于数组中的元素均不相等,且只缺失了一个,因此一种简单的思路是,求从0n的等差数列前n项和,减去数组元素累加的和,便可得到缺失的那个数。

若使用位运算,这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, …, n再次放入这个数组。

三. 示例代码

// 等差数列求和
class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int n = nums.size();
        if (n < 1) return 0;
        int completeSum = n * (n + 1) / 2, currSum = 0;
        for (int i = 0; i < n; ++i)
            currSum += nums[i];
        return completeSum - currSum;
    }
};
// 位运算
class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int ans = 0;
        for (vector<int>::size_type i = 0; i < nums.size(); ++i){
            ans ^= nums[i];
        }
        for (int i = 0; i <= nums.size(); ++i){
            ans ^= i;
        }
        return ans;
    }
};

四. 小结

该题目给定的条件算是比较宽松,比如数值不重复。直接使用等差数列求和法可以快速找到缺失数,只需几分钟实现,但没什么锻炼价值;可尝试使用异或运算来解这道题。

时间: 2024-11-05 12:29:34

leetcode笔记:Missing Number的相关文章

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,找出缺失的数字.如果数

&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

[LeetCode]71. 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 usi

LeetCode 268. 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 usi

LeetCode笔记--202Happy Number

1 题目描述 Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process un

Leetcode 268 Missing Number 位运算

题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组,这样就和singe number 一样. 1 class Solution { 2 public: 3 int missingNumber(std::vector<int>& nums) { 4 int ans = 0; 5 for (std::vector<int>::siz

LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告

1.题目大意 Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. Not

leetcode 268 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 usi

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

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. Example 1 Input: [3,0,1] Output: 2 Example 2 Input: [9,6,4,2,3,5,7,0,1] Output: 8 Note:Your algorithm should run in linear runtime c

[LeetCode]268 Missing Number

很简单,提供两种解法. (1)等差数列先求得总和,然后减去给定的队列,剩下的值就是缺失值. (2)先排个序,逐个比较,不等的直接return 第一种的代码: class Solution { public: int missingNumber(vector<int>& nums) { int n = nums.size(); int expect = (n+1) * n / 2; for(int i = 0; i < n; i ++) expect -= nums[i]; ret