The missing number of Leetcode (268), the number III that only appears once (260)

1. The missing number (268)

Title description:

【simple】

Given an array nums containing n numbers in [0, n], find the number in the range [0, n] that does not appear in the array.

Advanced:

Can you achieve linear time complexity and only use an extra constant space algorithm to solve this problem?

Example 1:

输入:nums = [3,0,1]
输出:2
解释:n = 3,因为有 3 个数字,所以所有的数字都在范围 [0,3] 内。2 是丢失的数字,因为它没有出现在 nums 中。

Example 2:

输入:nums = [0,1]
输出:2
解释:n = 2,因为有 2 个数字,所以所有的数字都在范围 [0,2] 内。2 是丢失的数字,因为它没有出现在 nums 中。

Topic link

Thinking analysis:

1, with the previous solution had only appear once figures would be similar, where you can also use the "exclusive OR operation" ingenious solution to the problem.
2. The associative law of exclusive OR operation:a ^ b ^ c = a ^ (b ^ c) = (a ^ b) ^ c;

  • 自反:a ^ b ^ a = b.

3. Requirements: The length of the array is n, find the missing range in [0,n] in
the array 4. If we XOR the array with [0,n] that is the subscript of the array + the length of the array, then the last The result is the missing number

answer:

def missingNumber(self, nums):
        missing = len(nums)
        for i, num in enumerate(nums):
            missing ^= i ^ num
        return missing
  • Time complexity: O (n) O(n)O ( n )
  • Space complexity: O (1) O (1)O ( 1 )

2. The number III that only appears once (260)

Title description:

[Medium]
Given an integer array nums, exactly two elements appear only once, and all other elements appear twice. Find the two elements that appear only once.

Topic link

Thinking analysis:

1. Since the array only has two elements appearing only once, the bitmask obtained by XORing all numbers is the difference between the two numbers. If the two numbers to be found are x and y, then bitmask = x ^ y.Insert picture description here

  • Can we extract x and y directly from the bitmask? No, but we can use bitmask as a marker to separate x and y.

2. From the question, a != b, all bitmask!= 0, so there must be 1 on the far right.

  • By bitmask & (-bitmask)reservation bitmask rightmost 1, this one from either x, or from y.
    Insert picture description here

  • x & (-x) (x and its negative number (inverse code +1) bitwise AND, the result is the rightmost 1 of the reserved bits, and the other 1 is set to 0.

  • Insert picture description here

3. Find the rightmost 1 and set it as diff, for example, the found 1 is in the 4th place diff = 00001000
4. Then the 4th place of x and the 4th place of y must be different, one is 0 and the other is 1. , But you don't know who is 0 and who is 1.
5. Get res from each number in the array nums[i] & diff, which can be divided into two groups at this time. For example, 11001000 & 00001000 = 00001000, another set of 11000000 & 00001000 = 00000000 = 0;
6. The result of the diff with the upper diff is not equal to 0 if the 4th digit is 1, and the diff with the upper diff equal to 0 if the 4th digit is 0;
7. At this time The entire array is divided into two groups, the fourth bit is a group of 1, and the fourth bit is a group of 0. At this time, x and y are
8 in the two groups, and one result is res, then the other result is res ^bitmask, for example res = x,x ^ bitmask=x ^x ^y = y;

answer:

class Solution:
    def singleNumber(self, nums: List[int]) -> List[int]:
        bitmask=0
        for num in nums:
            bitmask^=num
        diff =bitmask&(-bitmask)
        x=0
        for num in nums:
            if num & diff:
                x ^= num
        return [x,bitmask^x]
  • Time complexity: O (n) O(n)O ( n )

  • Space complexity: O (1) O (1)O ( 1 )

Guess you like

Origin blog.csdn.net/weixin_45666566/article/details/113042891