LeetCode (No.136) - appears only once digital

Given a non-empty array of integers, in addition to an element appears only once, the rest of each element appears twice. To find out that only appears once in the elements.

Description:

Your algorithm should have linear time complexity. You can not use the extra space to achieve it?

Example 1:
Input: [2,2,1]
Output: 1

Example 2:
Input: [4,1,2,1,2]
Output: 4

Ideas: one by one with a bit operation to

class Solution:
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return 
        c=nums[0]
        for i in range(1,len(nums)):
            c=c^nums[i]
        return c
Published 114 original articles · won praise 55 · views 80000 +

Guess you like

Origin blog.csdn.net/zuolixiangfisher/article/details/87477105