Skip to content

Commit fa79e44

Browse files
Added Day-23 Solution
1 parent 91ebef0 commit fa79e44

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Day-23_Single_Number_III.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
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.
3+
4+
Example:
5+
6+
Input: [1,2,1,3,2,5]
7+
Output: [3,5]
8+
Note:
9+
10+
The order of the result is not important. So in the above example, [5, 3] is also correct.
11+
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
12+
13+
'''
14+
class Solution:
15+
def singleNumber(self, nums):
16+
s = reduce(lambda a,b: a^b, nums, 0)
17+
nz = s & (s-1) ^ s
18+
num1 = reduce(lambda a,b: a^(b*(b & nz == nz)), nums, 0)
19+
return(num1, s ^ num1)

0 commit comments

Comments
 (0)