-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3315.cpp
More file actions
36 lines (35 loc) · 864 Bytes
/
3315.cpp
File metadata and controls
36 lines (35 loc) · 864 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
class Solution {
public:
int suitable(int num) {
vector<int> bits(32, 0);
int index = 0;
while (num) {
bits[index] = num & 1;
index++;
num >>= 1;
}
// find the first zero
int zeroIndex = -1;
for (int i = 0; i < 32; ++i) {
if (bits[i] == 0) {
zeroIndex = i;
break;
}
}
bits[zeroIndex - 1] = 0;
int res = 0;
for (int i = 0; i < 32; ++i) {
res += (bits[i] << i);
}
return res;
}
vector<int> minBitwiseArray(vector<int>& nums) {
int n = nums.size();
vector<int> res(n, -1);
for (int i = 0; i < n; ++i) {
if (nums[i] == 2) continue;
res[i] = suitable(nums[i]);
}
return res;
}
};