[LeetCode]136. Single Number

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

找出一个数组中唯一一个只出现了一次的数,其余的都出现了两次

 1 class Solution(object):
 2     def singleNumber(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: int
 6         """
 7         if len(nums)==1:
 8             return nums[0]
 9         num = 0
10         for i in nums:
11             num ^= i
12         return num

 

posted @ 2017-03-04 20:56  wilderness  阅读(93)  评论(0编辑  收藏  举报