forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
single-number-iii.py
45 lines (34 loc) · 964 Bytes
/
single-number-iii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Time: O(n)
# Space: O(1)
import operator
import collections
class Solution(object):
# @param {integer[]} nums
# @return {integer[]}
def singleNumber(self, nums):
x_xor_y = reduce(operator.xor, nums)
bit = x_xor_y & -x_xor_y
result = [0, 0]
for i in nums:
result[bool(i & bit)] ^= i
return result
class Solution2(object):
# @param {integer[]} nums
# @return {integer[]}
def singleNumber(self, nums):
x_xor_y = 0
for i in nums:
x_xor_y ^= i
bit = x_xor_y & ~(x_xor_y - 1)
x = 0
for i in nums:
if i & bit:
x ^= i
return [x, x ^ x_xor_y]
class Solution3(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
return [x[0] for x in sorted(collections.Counter(nums).items(), key=lambda i: i[1], reverse=False)[:2]]