Leetcode 136. Single Number

Question

Solution1

使用字典

1
2
3
4
5
6
7
8
9
10
11
class Solution:
def singleNumber(self, nums: List[int]) -> int:
dictt = {}
for num in nums:
if num not in dictt:
dictt[num] = 1
else:
dictt.pop(num)
for key in dictt:
if dictt[key] == 1:
return key

Solution2

采用和的方式

1
2
3
class Solution:
def singleNumber(self, nums: List[int]) -> int:
return 2*sum(set(nums)) - sum(nums)