leetcode--python--numbers that only appear once

136. Numbers that only appear once

Given an array of non-empty integers, except for an element that appears only once, every other element appears twice. Find the element that appears only once.
If you do not limit the space complexity and time complexity, this problem is very easy to do, you can use brainless traversal, you can use a hash table, but if you limit it, it will be difficult to do, the following method is very XOR Novelty, hereby recorded.

1. If any number is XORed with 00, the result is still the original number, that is, a \oplus 0=aa⊕0=a.  2. Perform an exclusive OR operation on any number and itself, and the result is 00, that is a \oplus a=0a⊕a=0. 3. The exclusive OR operation satisfies the commutative and associative laws, that is, a \oplus b \oplus a=b \oplus a \oplus a=b \oplus (a \oplus a)=b \oplus0=ba⊕b⊕a=b⊕a⊕a=b⊕(a⊕a)=b⊕0=b.

Through the above conclusions, because the title states: only one appears once, and the others appear twice, then we XOR the array in turn to get the last value that only appears once

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return(reduce(lambda x, y: x^y, nums))#reduce是python的内置函数,
        #reduce接受一个函数和一个序列化数据作为参数,相当于把序列化的值交给函数处理,
        #一般reduce会和lambda函数一起使用

260. Number III that only appears once

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.
Solution: In order to make the space complexity still O(1), the exclusive OR operation and the logical AND operation are used here. The exclusive OR operation has been mentioned above. The basic content of the logical AND operation is: For 2 binary numbers, only the same position If the value of is all 1, the corresponding bit after the logical AND is still 1, for example, 0010&1110 is obtained after 0010. For code explanation, please refer to the comment code below. Basic idea:

  1. First perform the exclusive OR operation on all the numbers in the array to get xor. Since only 2 are single numbers, the other numbers appear in pairs, so the result of the last exclusive OR operation is actually equivalent to only appearing once XOR of 2 numbers;
  2. Initialize ten to 1, because the first bit of the binary of 1 is 1, and other positions are 0, so when we shift, we can only be 1 after the position of 1 and xor are logically ANDed;
  3. Perform a loop, know that the logical AND operation of xor and tem is not 1, get tem, the purpose is to find the value of the position in xor, if the position of xor is 1, prove that the two numbers in the original array are 1 After the exclusive OR operation, the position is 1;
  4. After finding the tem whose position is 1, traverse nums, perform logical AND operation on all elements and tem, classify, and output
class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        xor = 0
        num1 = 0
        num2 = 0
        for num in nums:
            xor = xor ^ num
        tem = 1
        while not tem & xor:
            tem <<= 1#移位操作,0001移位之后变为0010,0010移位变为0100
        for num in nums:
            if tem & num:
                num1 ^= num
            else:
                num2 ^= num
        return([num1, num2]) 

Guess you like

Origin blog.csdn.net/AWhiteDongDong/article/details/111754136