-
Notifications
You must be signed in to change notification settings - Fork 43
/
single-number-iii.py
120 lines (101 loc) · 2.77 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# python 3
# https://leetcode.com/problems/single-number-iii/description/
# Time: O(n)
# Space: O(1)
#
# Given an array of numbers nums, in which exactly two
# elements appear only once and all the other elements
# appear exactly twice. Find the two elements that appear only once.
#
# For example:
#
# Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].
#
# Note:
# The order of the result is not important. So in the
# above example, [5, 3] is also correct.
# Your algorithm should run in linear runtime complexity.
# Could you implement it using only constant space complexity?
# V0
import collections
class Solution(object):
def singleNumber(self, nums):
num_count = collections.Counter(nums)
return [ i for i in num_count if num_count[i] == 1 ]
# V1
# V2
class Solution:
def singleNumber(self, nums):
no_repeat_array=[]
for index, item in enumerate(nums):
if item in no_repeat_array:
no_repeat_array.remove(item)
else:
no_repeat_array.append(item)
return no_repeat_array
# V3
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
return [x[0] for x in sorted(list(collections.Counter(nums).items()), key=lambda i: i[1], reverse=False)[:2]]
# V4
class Solution:
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
# V5
class Solution2:
# @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]
# V6
# 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]]