LeetCode Single Number Conclusion

Single Number Conclusion

Single Number problem is mainly directed against a class of problems bit computing. There are three types respectively, this mainly for three leetcode corresponding title, to answer, and bit-Manipulation related organize and summarize.

136. Single Number

Topic Description: Given a non-empty array of integers, in addition to an element appears only once, the rest of each element appears twice. Only to find out that there was once an element

This question there are two solutions: using a Hash table, since the efficiency of the hash table lookup is O (1), so that the time complexity is O (n), the spatial complexity is O (n).

This question requires space complexity is O (1), using bit-Manipulation, specifically using the bit XOR operation. This is because a ^ a = 0, a ^ 0 = a. It will become XORed 0. Finally, only occurs once for an integer number appears twice.

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int res=0;
        for(const int&num:nums)
            res^=num;
        return res;
    }
};

#### 137. The Single Number The II subject description: Given a non-empty array of integers, in addition to an element appears only once, the rest of each element appears three times. To find out that only appears once in the elements.

For this problem, but problems arise from the two become three times appear. It is necessary to tap the inherent logic.

Solution one: we can build a 32-bit number, to count the number of occurrences of each and every one, if a bit = 1, then if the integer appears three times, to take more than 3 to 0, we put every number corresponding bits are combined to take more than 3, the last remaining number is the single digits.

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int res=0大专栏  LeetCode Single Number Conclusionpan class="p">;
        for(int i=0;i<32;i++){
            int sum=0;
            for(int j=0;j<nums.size();j++){
                sum+=(nums[j]>>i)&1;
            }
            res|=(sum%3)<<i;
        }
        return res;
    }
};

Solution two: indicates the case you INT appears with three integers, one means one, two represent appears twice. When three appearances, the bit is cleared. So one value when the final answer.

Suppose now that there is a digital one, then one way to update the XOR figure 1 (0 ^ 0,1 ^ 1 = 1 = 0), while the update method is one of two spend a state in the figure (1 & 1 = 1,1 & 0 = 0). Note that update is the first update order two, and then update One. And then update the three, has been updated due before the two then updated one, if at this time two = 1, one also figure 1. So this time there were three, two and one needs to be set to 0, so one and two and the Instead of the number three.

class Solution { public: int singleNumber(vector & nums) { int one=0,two=0,three=0; for(int&num:nums){ two|=one&num; one=one^num; three=one&two; one&=~three; two&=~three; } return one; } };

Guess you like

Origin www.cnblogs.com/liuzhongrong/p/11961083.html