LeetCode algorithm bit operations—numbers that appear only once

Table of contents

136. Numbers that appear only once - LeetCode

Problem-solving ideas:

Code:

operation result:

Replenish

 Important properties of XOR


136. Numbers that appear only once - LeetCode

Give you a non-empty integer array nums , except that a certain element only appears once, the rest Each element appears twice. Find the element that appears only once.

You must design and implement a linear-time algorithm to solve this problem that uses only constant extra space.

Example 1:

Import:nums = [2,2,1]
Exit: 1

Example 2:

Import:nums = [4,1,2,1,2]
Exit: 4

Example 3:

Import:nums = [1]
Exit: 1

hint:

  • 1 <= nums.length <= 3 * 104
  • -3 * 104 <= nums[i] <= 3 * 104
  • Every element appears twice except for one element which appears only once.

解题思路:

Properties of XOR operation (^):

  • The XOR operation satisfies the commutative law and associative law, that is, a^b^c = a^(b^c).
  • For any number x, x^x=0, that is, the result of a number XORed with itself is 0.
  • For any number x, x^0=x, that is, the result of a number XORed with 0 is itself.

With these properties in mind, let's take a look at the execution of the code:

  1. Initialize variable a to 0.
  2. Iterate through each element in the array nums:
    • Perform an XOR operation on a and the current element, that is, a^=nums[i].
    • The XOR operation will eliminate numbers that appear twice, because the result of XORing two identical numbers is 0.
    • The XOR operation will leave behind numbers that appear only once, because the result of any number XORed with 0 is itself.
    • Eventually, a will hold numbers that appear only once.
  3. Return variable a as the result.

Now, let's analyze this code through a case:

Assume that the input array nums is [2, 4, 2, 1, 4], in which only the number 1 appears once, and other numbers appear twice.

The execution process is as follows:

  1. Initialize variable a to 0.
  2. Traverse the array nums:
    • The first loop: a^=2, the value of a becomes 2.
    • Second loop: a^=4, the value of a becomes 6.
    • The third cycle: a^=2, the value of a becomes 4.
    • The fourth cycle: a^=1, the value of a becomes 5.
    • The fifth loop: a^=4, the value of a becomes 1.
  3. Returns the value 1 of a as the result.

代码:

class Solution {
    public int singleNumber(int[] nums) {
        int a=0;
        for(int i=0;i<nums.length;i++){
            a^=nums[i];
        }
        return a;
    }
}

运行结果:

Replenish

 Important properties of XOR

  1. Associative law:The exclusive OR operation satisfies the associative law, that is, for any three numbers a, b and c, (a ^ b) ^ c = a ^ (b ^ c).

  2. Reflexivity: The result of the XOR operation of any number with itself is 0, that is, a ^ a = 0. This is because the XOR operation can be regarded as "addition without carry", and the result of the XOR operation on the same bits is 0.

  3. Zero element:0 is the zero element of the XOR operation, that is, for any number a, a ^ 0 = a is satisfied. This is because in the XOR operation, the result of any number XOR operation with 0 is itself.

  4. Inverse element: Every number has an inverse element in the XOR operation, that is, for any number a, there is a number b that satisfies a ^ b = 0. This means that the effect of a number can be undone via an XOR operation.

  5. Eliminate identical elements:When there are pairs of identical elements in the array, perform an XOR operation on the entire array, and the final result is 0. This is because the result of XOR of the same elements is 0, and the XOR operation satisfies the commutative law and associative law.

These properties make the XOR operation very useful in many scenarios, such asfinding numbers that only appear once, determining whether two numbers are equal, and exchanging the values ​​of two variables a> etc. By taking advantage of these properties, problem processing and solution can be simplified.

おすすめ

転載: blog.csdn.net/qq_62799214/article/details/132778743