logo头像
Snippet 博客主题

【Leetcode 136】Single Number

难度: 简单(Easy)

题目

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

思路

两个相同的数字异或为零

代码

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

评论系统未开启,无法评论!