[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?

解题思路

思路1:0-n求和,再减去数组元素的总和,即为缺失元素。
思路2:亦或操作。两个相同的数亦或结果为0,一个不为0的数与0亦或,其值为本身。

实现代码

C++代码1:

// Runtime: 36 ms
// 代码已AC,但求和有溢出风险。
class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int len = nums.size();
        int sum = 0;
        for_each(nums.begin(), nums.end(), [&sum](int n){ sum += n; });
        int total = (len + 1) / 2.0 * (0 + len);
        return total - sum;
    }
};

C++代码2:

// Runtime: 36 ms
class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int miss = nums[0] ^ 0;
        for (int i = 1; i < nums.size(); i++) {
            miss ^= nums[i];
            miss ^= i;
        }

        return miss ^= nums.size();
    }
};

Java代码1:

// Runtime: 1 ms
public class Solution {
    public int missingNumber(int[] nums) {
        int sum = (int)((nums.length + 1) / 2.0 * nums.length);
        for (int n : nums) {
            sum -= n;
        }

        return sum;
    }
}

Java代码2:

// Runtime: 1 ms
public class Solution {
    public int missingNumber(int[] nums) {
        int miss = nums[0] ^ 0;
        for (int i = 1; i < nums.length; i++) {
            miss ^= nums[i];
            miss ^= i;
        }

        return miss ^= nums.length;
    }
}
时间: 2024-08-30 09:30:03

[LeetCode] Missing Number的相关文章

[LeetCode] Binary Number with Alternating Bits 有交替位的二进制数

Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. Example 1: Input: 5 Output: True Explanation: The binary representation of 5 is: 101 Example 2: Input: 7 Output: False Ex

[LeetCode]191.Number of 1 Bits

题目 Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so the function should r

[LeetCode]200.Number of Islands

题目 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded

[LeetCode] Ugly Number II

Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. Note that 1 is typically treated as an

[LeetCode] Palindrome Number &amp;amp; Valid Palindrome - 回文系列问题

题目概述: Determine whether an integer is a palindrome. Do this without extra space. 题目分析: 判断数字是否是回文 例如121.656.3443 方法有很多,正着看和到着看两数相同:当然负数显然不是回文 我的方法: 第一种方法: 由于没有没有看到前面的without extra space.采用的方法是把数字转换为字符串,依次比较最前和最后两个字符是否相同,直到遍历完毕. /** * 判断一个数字是否是回文数字 Pal

[LeetCode] Happy Number

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 until the

[LeetCode] Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解题思路 考虑全部用二进制表示,如果我们把 第 ith 个位置上所有数字的

[LeetCode] Single Number III

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The order

[LeetCode] Ugly Number

Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1 is ty