您的位置:首页 > 编程语言 > Python开发

leetcode Single Number python

2015-12-19 12:09 573 查看
#question : 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?

class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
intlen=len(nums)
if intlen == 0:
return -1
if intlen == 1:
return nums[0]

ans=nums[0]
for i in range(1,intlen):
ans = ans ^ nums[i]

return ans
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: