forked from KnowledgeCenterYoutube/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
260_Single_Number_iii
48 lines (43 loc) · 1.05 KB
/
260_Single_Number_iii
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
Leetcode 260: Single Number III
Detailed video explanation: https://youtu.be/L-EaPf5tD5A
============================================
C++:
----
vector<int> singleNumber(vector<int>& nums) {
int xy = 0;
for(int n: nums) xy ^= n;
xy &= -xy;
vector<int> result = {0, 0};
for(int n: nums){
if(xy & n) result[0] ^= n;
else result[1] ^= n;
}
return result;
}
Java:
-----
class Solution {
public int[] singleNumber(int[] nums) {
int xy = 0;
for(int n: nums) xy ^= n;
xy &= -xy;
int[] result = new int[2];
for(int n: nums){
if((xy & n) == 0) result[0] ^= n;
else result[1] ^= n;
}
return result;
}
}
Python3:
-------
class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
xy = 0
for n in nums: xy ^= n
xy &= -xy
result = [0]*2
for n in nums:
if (xy & n) == 0: result[0] ^= n
else: result[1] ^= n
return result