LeetCode: missing num, count of 1s

Missing Number

[Problem]

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?

[Solution2] sum: Time~O(n) Space~O(1) //overflow risk
missing = expected_sum - actual_sum = (0+n)(n+1)/2 - sum

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

[Solution1] xor: Time~O(n) Space~O(1) //no overflow risk
xor all [i] and 0 to n: i^i=0, 0^missing=missing => final result = missing
[Tip]
- do not use for(auto...) since need to use counter "i"
- 0~n: n+1 numbers, nums[0~n-1]: n items => "for(i=0~n-1) res^=i^nums[i]" will not xor "n" => must init res=n

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        //sol2: xor all items and 0~n => (i^i)=0 so 0^missing=missing
        int n = nums.size(), res = n;
        for(int i=0; i<n; ++i) res ^= i ^ nums[i];
        return res;
    }
};
时间: 2024-10-31 18:06:44

LeetCode: missing num, count of 1s的相关文章

LeetCode(38)题解: Count and Say

https://leetcode.com/problems/count-and-say/ 题目: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as

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

[LeetCode] Missing Ranges 缺失区间

Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing ranges.For example, given [0, 1, 3, 50, 75], return [“2”, “4->49”, “51->74”, “76->99”] 这道题让我们求缺失区间,跟之前那道Summary Ranges很类似,这道题让我们求缺失的空间,给了一个空间的范围[lo

LeetCode OJ:Count and Say(数数)

The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211. Given an

【leetcode】204 - Count Primes

Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity

LeetCode——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. Note:Your algorithm should run in linear runtime complexity. Could you imp

Leetcode题目:Count and Say

题目:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211. Given

leetcode笔记:Count and Say

一.题目描述 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, - 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2", then "one 1"

LeetCode解题报告-- Count and Say

题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, - 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or 1211. Give