Leetcode Brushing Questions (Array·Bit XOR) 16—Numbers that appear only once II

137. Numbers that occur only once II

Given a non-empty array of integers, each element appears three times except for one element. Find the element that appears only once.
Explanation:
Your algorithm should have linear time complexity. Can you do it without using extra space?

analyze

This code is written with reference to the leetcode question. I will analyze the origin of the code according to my understanding.
This code has O(n) time complexity and O(1) space complexity.

First analyze the topic: a non-empty integer array, except for one element that appears once, the other elements appear three times-find the element that appears once.
That is: we need to distinguish elements that appear once and three times, but when we traverse, we need to traverse one by one, so in addition to detecting elements that appear once and three times, there will also be an intermediate state: elements that appear twice . Because there are three states in total, it is represented by two binary numbers: 01, 10, and 00 respectively represent the states of elements that appear once, twice, and three times
; Need to consider the out-of-order problem of the original array)
We need to find an operation method that satisfies:
00 encounters one 1 and becomes 01, 01 encounters one 1 and becomes 10, 10 encounters one 1 and becomes 00, encounters 0 unchanged

truth table

XY Z Xnew Ynew
00 0 0 0
01 0 0 1
10 0 1 0
00 1 0 1
01 1 1 0
10 1 0 0

XY: original state
Z: input value
My understanding here is: 1 is a general term. From the representation of the three states, it can be seen that Xnew = 1 means a number that appears twice (of course, because it is an out-of-order array, if the previous input is not If the full three is satisfied and the state is cleared, the number that appears twice is not represented here); similarly, Ynew = 1 represents the number that appears once.
Calculation logic expression: (add rows where Ynew = 1)
Ynew = (~X) Y (~Z)+ (~X) (~Y) Z = (~X) [Y ( ~Z) + (~Y) Z] = (~X) (Y^Z)
Xnew = (~Ynew) (X^Z)

class Solution {
    
    
    public int singleNumber(int[] nums) {
    
    
        int sinceOne = 0,sinceTwo = 0;
        // String resOne,resTwo;
        for(int num : nums){
    
    
            // resOne = Integer.toBinaryString(sinceOne);
            // resTwo = Integer.toBinaryString(sinceTwo);
            // System.out.println("resone:"+resOne);
            // System.out.println("restwo:"+resTwo);
            // System.out.println(" ");
            sinceOne = ~sinceTwo & (sinceOne ^ num);
            sinceTwo = ~sinceOne & (sinceTwo ^ num);
            
        }
        // resOne = Integer.toBinaryString(sinceOne);
        // resTwo = Integer.toBinaryString(sinceTwo);
        // System.out.println("resone:"+resOne);
        // System.out.println("restwo:"+resTwo);
        return sinceOne;
    }
}

Source: LeetCode
Link: https://leetcode-cn.com/problems/single-number-ii
Copyright belongs to LeetCode Network. For commercial reprints, please contact the official authorization, for non-commercial reprints, please indicate the source.

try to draw inferences

If this article is changed to except for a certain element that only appears once, each of the other elements appears four times. Find the element that appears only once.
The state is: 0 times, 1 time, 2 times, 3 times: can be represented by two bits of binary,
corresponding to: 00, 01, 10, 11
truth table:

XY Z Xnew Ynew
00 0 0 0
01 0 0 1
10 0 1 0
11 0 1 1
00 1 0 1
01 1 1 0
10 1 1 1
11 1 0 0

Ynew = ( X)Y( Z) + XY(~Z) + ( X)( Y)Z + X(~Y)Z = ( X)[Y( Z) + (~Y)Z] + X[Y (~Z) + (~Y)Z] = (~X)(Y^Z) + X(Y^Z) = (Y^Z) Xnew
= X^Z
It can be seen from the logical expression that it can appear once Use Y alone to represent, which is the same as the solution to the question that distinguishes between once and twice

Guess you like

Origin blog.csdn.net/qq_38754625/article/details/108468986