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

【LintCode 简单】82. 落单的数

2018-01-16 11:27 239 查看
1.问题描述:

给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字。

2.样例:

给出 [1,2,2,1,3,4,3],返回 4

挑战 

一次遍历,常数级的额外空间复杂度

3.代码:

class Solution:
"""
@param: A: An integer array
@return: An integer
"""
"""
def singleNumber(self, A):
# write your code here
length = len(A)
for i in range(length):
fg=True
for j in range(length):
if A[i]==A[j] and i!=j:
fg=False
break
if fg:
return A[i]
"""
#异或运算
def singleNumber(self, A):
# write your code here
length = len(A)
ans=0
for i in range(length):
ans^=A[i]
return ans

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