您的位置:首页 > 其它

LeetCode:Single Number

2014-10-15 15:57 323 查看
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?

一个数组中都是两两重复的数字,除了一个不重复。找出这个不重复的。

使用异或,如果两个相同数字异或后是0,0和其他数字异或是其他数字。且异或满足交换律。

class Solution:
# @param A, a list of integer
# @return an integer
def singleNumber(self, A):
x=0
for i in A:
x=x^i
return x
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: