-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy path137_SingleNumber-2
More file actions
41 lines (35 loc) · 1018 Bytes
/
137_SingleNumber-2
File metadata and controls
41 lines (35 loc) · 1018 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
40
41
// C++:
int singleNumber(vector<int>& nums) {
int ones = 0, twos = 0, thrice = 0;
for(int n : nums){
twos = twos | (ones & n);
ones = ones ^ n;
thrice = ones & twos;
ones &= ~thrice;
twos &= ~thrice;
}
return ones;
}
// Java:
public int singleNumber(int[] nums) {
int ones = 0, twos = 0, thrice = 0;
for(int n : nums){
twos = twos | (ones & n);
ones = ones ^ n;
thrice = ones & twos;
ones &= ~thrice;
twos &= ~thrice;
}
return ones;
}
## Python3:
def singleNumber(self, nums: List[int]) -> int:
ones, twos, thrice = 0, 0, 0
for n in nums:
twos = twos | (ones & n)
ones = ones ^ n
thrice = ones & twos
ones &= ~thrice
twos &= ~thrice
return ones
Detailed Video Explanation is available here: https://youtu.be/ZbTXZ2_YAgI