File tree Expand file tree Collapse file tree 1 file changed +87
-0
lines changed Expand file tree Collapse file tree 1 file changed +87
-0
lines changed Original file line number Diff line number Diff line change
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 ]
You can’t perform that action at this time.
0 commit comments