Approach
# 要找到從右邊算過來,最左邊的連續1把它減掉,這樣子OR出來就會是答案
Time Complexity
O(n)
Space Complexity
O(n)
Code
class Solution {
public:
vector<int> minBitwiseArray(vector<int>& nums) {
vector<int> ans;
for (int n : nums) {
if (n % 2 == 0) {
ans.push_back(-1);
}
else {
int tmp = n, bitCount = 0;
while ((tmp & 1) == 1) {
tmp >>= 1;
bitCount++;
}
ans.push_back(n - (1 << (bitCount - 1)));
}
}
return ans;
}
};