Skip to content

Commit eba5782

Browse files
committed
upload solutions to 136. Single Number
1 parent f1e006d commit eba5782

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

136.single-number.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# EASY
2+
3+
# first submission
4+
# runtime: 129 ms O(n)
5+
# memory: 19 MB
6+
class Solution:
7+
def singleNumber(self, nums: list[int]) -> int:
8+
dict = {}
9+
for num in nums:
10+
if num in dict.keys():
11+
dict.pop(num)
12+
continue
13+
14+
dict[num] = 1
15+
16+
return dict.popitem()[0]
17+
18+
19+
# second submission
20+
# runtime: 124 ms O(n)
21+
# memory: 18.8 MB
22+
# src: https://medium.com/@punitkmr/single-number-fe30e4f6693d
23+
class Solution:
24+
def singleNumber(self, nums: list[int]) -> int:
25+
dict = {}
26+
for num in nums:
27+
try:
28+
dict.pop(num)
29+
except:
30+
dict[num] = 1
31+
32+
return dict.popitem()[0]
33+
34+
35+
# third submission
36+
# runtime: 128 ms O(n)
37+
# memory: 19 MB
38+
# src: https://medium.com/@punitkmr/single-number-fe30e4f6693d
39+
class Solution:
40+
def singleNumber(self, nums: list[int]) -> int:
41+
return (2 * sum(set(nums))) - sum(nums)
42+
43+
44+
# fourth submission
45+
# runtime: 771 ms O(n^2)
46+
# memory: 19.1 MB
47+
class Solution:
48+
def singleNumber(self, nums: list[int]) -> int:
49+
numlist = []
50+
for num in nums:
51+
if num in numlist:
52+
numlist.remove(num)
53+
continue
54+
55+
numlist.append(num)
56+
57+
return numlist[0]
58+
59+
60+
# fifth submission
61+
# runtime: 128 ms O(n)
62+
# memory: 18.8 MB
63+
# src: https://medium.com/@punitkmr/single-number-fe30e4f6693d
64+
class Solution:
65+
def singleNumber(self, nums: list[int]) -> int:
66+
single_num = 0
67+
for num in nums:
68+
single_num ^= num
69+
70+
return single_num
71+
72+
73+
# sixth submission
74+
# runtime: 137 ms O(n)
75+
# memory: 18.6 MB
76+
# src: https://dzone.com/articles/optimal-solution-single-number-leetcode-problem
77+
class Solution:
78+
def singleNumber(self, nums: list[int]) -> int:
79+
nums.sort()
80+
idx = 0
81+
while idx < len(nums) - 2:
82+
if nums[idx] != nums[idx + 1]:
83+
return nums[idx]
84+
85+
idx += 2
86+
87+
return nums[idx]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy