-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_number_II_code.cpp
102 lines (96 loc) · 2.66 KB
/
single_number_II_code.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Question Link - https://leetcode.com/problems/single-number-ii/description/
// Solution
class Solution {
public:
int singleNumber(vector<int>& nums) {
vector<long long> numsp, numsn;
for(long long i=0; i<nums.size(); i++)
{
if(nums[i]>=0)numsp.push_back(1ll*nums[i]);
else numsn.push_back(abs(1ll*nums[i]));
}
vector<long long> bits(64,0);
for(long long i=0; i<numsp.size(); i++)
{
long long j=0;
long long x = 1;
long long byt = numsp[i];
while(byt>0)
{
bits[j] += byt&1;
byt = byt>>1;j++;
}
}
long long ans1 = 0;
for(long long j=0; j<bits.size(); j++)
{
if(bits[j]%3) ans1 += (1<<j);
}
bits.clear();
bits.resize(64,0);
for(long long i=0; i<numsn.size(); i++)
{
long long j=0;
long long x = 1;
long long byt = numsn[i];
while(byt>0)
{
bits[j] += byt&1;
byt = byt>>1;j++;
}
}
long long ans2 = 0;
for(long long j=0; j<bits.size(); j++)
{
if(bits[j]%3) ans2 += (1<<j);
}
if(ans1==0) return -ans2;
else if(ans2==0) return ans1;
else return 0;
}
};
/// Both solutions are important
class Solution {
public:
int singleNumber(vector<int>& nums) {
vector<long long> numsp, numsn;
for(long long i=0; i<nums.size(); i++)
{
if(nums[i]>=0)numsp.push_back(1ll*nums[i]);
else numsn.push_back(abs(1ll*nums[i]));
}
long long a=LLONG_MAX, b=0, c=0;
for(int i=0; i<numsp.size(); i++)
{
long long aa = numsp[i]&a;
long long bb = numsp[i]&b;
long long cc = numsp[i]&c;
a = a|cc;
a = a & (~aa);
b = b|aa;
b = b & (~bb);
c = c|bb;
c = c & (~cc);
}
// return b;
long long x = b;
a=LLONG_MAX, b=0, c=0;
for(long long i=0; i<numsn.size(); i++)
{
long long aa = numsn[i]&a;
long long bb = numsn[i]&b;
long long cc = numsn[i]&c;
a = a|cc;
a = a & (~aa);
b = b|aa;
b = b & (~bb);
c = c|bb;
c = c & (~cc);
}
// cout<<x<<" "<<b<<endl;
if(x == 0 && b != 0) return -b;
else if(b == 0 && x != 0) return x;
else return 0;
}
};
// For understanding follow https://www.youtube.com/watch?v=3gJxLkPPW6M