3315. 构造最小位运算数组 II
题目描述
给你一个长度为 n
的质数数组 nums
。你的任务是返回一个长度为 n
的数组 ans
,对于每个下标 i
,以下 条件 均成立:
ans[i] OR (ans[i] + 1) == nums[i]
除此以外,你需要 最小化 结果数组里每一个 ans[i]
。
如果没法找到符合 条件 的 ans[i]
,那么 ans[i] = -1
。
质数 指的是一个大于 1 的自然数,且它只有 1 和自己两个因数。
示例 1:
输入:nums = [2,3,5,7]
输出:[-1,1,4,3]
解释:
- 对于
i = 0
,不存在ans[0]
满足ans[0] OR (ans[0] + 1) = 2
,所以ans[0] = -1
。 - 对于
i = 1
,满足ans[1] OR (ans[1] + 1) = 3
的最小ans[1]
为1
,因为1 OR (1 + 1) = 3
。 - 对于
i = 2
,满足ans[2] OR (ans[2] + 1) = 5
的最小ans[2]
为4
,因为4 OR (4 + 1) = 5
。 - 对于
i = 3
,满足ans[3] OR (ans[3] + 1) = 7
的最小ans[3]
为3
,因为3 OR (3 + 1) = 7
。
示例 2:
输入:nums = [11,13,31]
输出:[9,12,15]
解释:
- 对于
i = 0
,满足ans[0] OR (ans[0] + 1) = 11
的最小ans[0]
为9
,因为9 OR (9 + 1) = 11
。 - 对于
i = 1
,满足ans[1] OR (ans[1] + 1) = 13
的最小ans[1]
为12
,因为12 OR (12 + 1) = 13
。 - 对于
i = 2
,满足ans[2] OR (ans[2] + 1) = 31
的最小ans[2]
为15
,因为15 OR (15 + 1) = 31
。
提示:
1 <= nums.length <= 100
2 <= nums[i] <= 109
nums[i]
是一个质数。
解法
方法一:位运算
对于一个整数 $a$,满足 $a \lor (a + 1)$ 的结果一定为奇数,因此,如果 $\text{nums[i]}$ 是偶数,那么 $\text{ans}[i]$ 一定不存在,直接返回 $-1$。本题中 $\textit{nums}[i]$ 是质数,判断是否是偶数,只需要判断是否等于 $2$ 即可。
如果 $\text{nums[i]}$ 是奇数,假设 $\text{nums[i]} = \text{0b1101101}$,由于 $a \lor (a + 1) = \text{nums[i]}$,等价于将 $a$ 的最后一个为 $0$ 的二进制位变为 $1$。那么求解 $a$,就等价于将 $\text{nums[i]}$ 的最后一个 $0$ 的下一位 $1$ 变为 $0$。我们只需要从低位(下标为 $1$)开始遍历,找到第一个为 $0$ 的二进制位,如果是第 $i$ 位,那么我们就将 $\text{nums[i]}$ 的第 $i - 1$ 位变为 $1$,即 $\text{ans}[i] = \text{nums[i]} \oplus 2^{i - 1}$。
遍历所有的 $\text{nums[i]}$,即可得到答案。
时间复杂度 $O(n \times \log M)$,其中 $n$ 和 $M$ 分别是数组 $\text{nums}$ 的长度和数组中的最大值。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
Python3
class Solution:
def minBitwiseArray(self, nums: List[int]) -> List[int]:
ans = []
for x in nums:
if x == 2:
ans.append(-1)
else:
for i in range(1, 32):
if x >> i & 1 ^ 1:
ans.append(x ^ 1 << (i - 1))
break
return ans
Java
class Solution {
public int[] minBitwiseArray(List<Integer> nums) {
int n = nums.size();
int[] ans = new int[n];
for (int i = 0; i < n; ++i) {
int x = nums.get(i);
if (x == 2) {
ans[i] = -1;
} else {
for (int j = 1; j < 32; ++j) {
if ((x >> j & 1) == 0) {
ans[i] = x ^ 1 << (j - 1);
break;
}
}
}
}
return ans;
}
}
C++
class Solution {
public:
vector<int> minBitwiseArray(vector<int>& nums) {
vector<int> ans;
for (int x : nums) {
if (x == 2) {
ans.push_back(-1);
} else {
for (int i = 1; i < 32; ++i) {
if (x >> i & 1 ^ 1) {
ans.push_back(x ^ 1 << (i - 1));
break;
}
}
}
}
return ans;
}
};
Go
func minBitwiseArray(nums []int) (ans []int) {
for _, x := range nums {
if x == 2 {
ans = append(ans, -1)
} else {
for i := 1; i < 32; i++ {
if x>>i&1 == 0 {
ans = append(ans, x^1<<(i-1))
break
}
}
}
}
return
}
TypeScript
function minBitwiseArray(nums: number[]): number[] {
const ans: number[] = [];
for (const x of nums) {
if (x === 2) {
ans.push(-1);
} else {
for (let i = 1; i < 32; ++i) {
if (((x >> i) & 1) ^ 1) {
ans.push(x ^ (1 << (i - 1)));
break;
}
}
}
}
return ans;
}