<LeetCode OJ> 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.

Note:

Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

分析:DONE

我真蛋疼。题意理解错了,我还以为是干嘛呢!

后来才明确原来是随机从0到size()选取了n个数,当中仅仅有一个丢失了(显然的)。

别人的算法:数学推出,0到size()的总和减去当前数组和sum

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int sum = 0;
        for(int num: nums)
            sum += num;
        int n = nums.size();
        return (n * (n + 1))/ 2 - sum;
    }
};

这道问题被标注为位运算问题:參考讨论区的位运算解法:

这个异或运算曾经用到过,到这道题还是想不起这种方法,我真是日了狗了!

异或运算xor。

0 ^ a = a ^ 0 =a

a ^ b = b ^ a

a ^ a = 0

0到size()间的全部数一起与数组中的数进行异或运算,

由于同则0,0异或某个未出现的数将存活下来

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int res = 0;
        for (int i = 1; i <= nums.size(); i++)
            res =res ^ i ^ nums[i-1];
        return res;
    }
};

注:本博文为EbowTang原创,兴许可能继续更新本文。

假设转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50457902

原作者博客:http://blog.csdn.net/ebowtang

时间: 2024-10-18 04:57:36

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

268. Missing Number (binary)

0到n的sum减去已经存在的就是missing number 1 //Old 2 class Solution { 3 public int missingNumber(int[] nums) { 4 int max = nums.length; 5 List<Integer> A = new ArrayList<Integer>(); 6 for(int i = 0; i <= max; i++) { 7 A.add(i); 8 } 9 for(int j = 0; j &

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 位运算

题意:先将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