LEETCODE 136.Singel Number

来源:互联网 发布:java验证码识别 编辑:程序博客网 时间:2024/06/01 10:43

Given an array of integers, every element appears twice except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

大意:

找出数组中只出现一次的元素,其余的都是出现两次

注意:

你的算法需要有一个线性的复杂度。即o(n)。最好不要使用额外的memory来实现


思路: 1 ^ 1 ^ 2 ^ 2 ^ 3 = 3


class Solution(object):    def singleNumber(self, nums):        """        :type nums: List[int]        :rtype: int        """        result = nums[0]        for i in xrange(1, len(nums)):            result = result ^ nums[i]        return result

0 0