-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0136-single-number.py
More file actions
39 lines (27 loc) · 836 Bytes
/
0136-single-number.py
File metadata and controls
39 lines (27 loc) · 836 Bytes
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
from typing import List
class Solution:
def singleNumber(self, nums: List[int]) -> int:
singletons = []
for num in nums:
if num not in singletons:
singletons.append(num)
else:
singletons.remove(num)
return singletons[0]
def singleNumber_hash(self, nums: List[int]) -> int:
isSingle = {}
for num in nums:
try:
isSingle.pop(num)
except:
isSingle[num] = True
return isSingle.popitem()[0]
def singleNumber_math(self, nums: List[int]) -> int:
return (2 * sum(set(nums))) - sum(nums)
def main():
sol = Solution()
input1 = [2, 2, 1]
input2 = [4, 1, 2, 1, 2]
print(sol.singleNumber(input2))
if __name__ == "__main__":
main()