leedcode题库_-2027-1-21:LeetCode每日一题 - (i - 1)) --&gt

📅 2026-11-30 👤 拼多多助力营销技巧 📂 手赚APP 👁️ 347 次阅读

2026-1-21:LeetCode每日一题

给你一个长度为 n 的 质数 数组 nums 。你的任务是返回一个长度为 n 的数组 ans ,对于每个下标 i ,以下 条件 均成立:

ans[i] OR (ans[i] + 1) == nums[i]

除此以外,你需要 最小化 结果数组里每一个 ans[i] 。

如果没法找到符合 条件 的 ans[i] ,那么 ans[i] = -1 。

质数 指的是一个大于 1 的自然数,且它只有 1 和自己两个因数。

x|(x+1)一定为奇数。OR是按位或运算。

方法一:

解题思路:

首先,将每一个num转换为二进制形式。我们这里用一个数组来存储。通过遍历这个数组来实现查找从右边数第一个0右边的1,再将它变为0。

举例:5[101]->4[100],因为(4)|(4+1)=[100]|[101]->[101]

7[111]->3[11],因为(3)|(3+1)=[11]|[100]->[111]

11[1011]->9[1001],因为(9)|(9+1)=[1001]|[1010]->[1011]

规律就是这样。如果全是1就把最高位的1变为0,如果末尾是0,说明它是一个偶数。

完整代码

#include

#include

using namespace std;

class Solution

{

public:

vectorminBitwiseArray(vector&nums)

{

vectorans;

for (int num : nums)

{

// 2 是特殊情况(偶数)

if (num == 2)

{

ans.push_back(-1);

continue;

}

vectorbits;

int x = num;

// 将 num 转换为二进制位

while (x > 0)

{

bits.insert(bits.begin(), x % 2);

x /= 2;

}

int res = -1;

int j = bits.size() - 1;

while (j >= 0)

{

// 如果当前位是 1,且不是最高位

if (bits[j] == 1 && j != 0)

{

j--;

}

// 如果当前位是 0,且不是最低位

else if (bits[j] == 0 && j != bits.size() - 1)

{

bits[j + 1] = 0;

int temp = 0;

for (int bit : bits)

{

temp = temp * 2 + bit;

}

res = temp;

break;

}

// 如果当前位是 1,且是最高位

else if (bits[j] == 1 && j == 0)

{

bits[j] = 0;

int temp = 0;

for (int bit : bits)

{

temp = temp * 2 + bit;

}

res = temp;

break;

}

}

ans.push_back(res);

}

return ans;

}

};

缺点就是时间效率和空间效率都很低。

方法二:

解题思路

不再使用数组转二进制,而是直接通过<<,>>,&,~这些运算,大致思路不变。

知识补充:

<<:左移运算

>>:右移运算

|:按位或运算

&:按位与运算

~:按位反运算

完整代码:

#include

#include

using namespace std;

class Solution

{ 拼多多帮助力入口

public:

vectorminBitwiseArray(vector&nums)

{

vectorans;

for (int num : nums)

{

if (num == 2)

{

ans.push_back(-1);

continue;

}

int res = -1;

int i = 0;

int highest = 0;

int temp = num;

while (temp > 1)

{

temp >>= 1;

highest++;

}

while (i <= highest)

{

// 情况1:当前位是1,且不是最高位 → 跳过

if ((num & (1 << i)) && (i != highest))

{

i++;

}

// 情况2:当前位是0,且不是最低位 → 把0右侧的1置为0

else if (!(num & (1 << i)) && (i != 0))

{

res = num & ~(1 << (i - 1)); // 把i-1位(0右侧的1)置0

break;

}

// 情况3:当前位是1,且是最高位 → 把该1置为0

else if ((num & (1 << i)) && (i == highest))

{

res = num & ~(1 << i); // 把最高位的1置0

break;

}

else

{

res = -1;

break;

}

}

ans.push_back(res);

}

return ans;

}

};

解析:

1.获取最高位

int highest_bit = 0;

int temp = num;

while (temp > 1) {

temp >>= 1;//右移一位

highest_bit++;

}

例如5[101]>>1 --> [10],highest_bit = 1,然后[10]>>1 --> [1],highest_bit = 2,[1]>>1 --> 0,highest_bit = 3,退出循环。

2. 遍历跳过连续的 1

if ((num & (1 << i)) && (i != heightest_num))

{

i ++;

}

模拟一下(num & (1 << i))这一部分:假设num是5[101]。

[101] & (1 << i),i = 0,-->[101] & [001] = [001],然后[101] & (1 << i),i = 1,-->[101] & [010] = [000],退出循环,此时 i = 1。

3. 非最低位的 0 → 置 0 右侧的 1

else if (!(num & (1 << i)) && (i != 0)) {

res = num & ~(1 << (i - 1));

break;

}

此时i=1,(!(num & (1 << i)) && (i != 0)) == 1,开始执行res = num & ~(1 << (i - 1));

1 << (i - 1) --> [001],

~(1 << (i - 1) -->[110],

num & ~(1 << (i - 1)) --> [101] & [110] --> [100]刚好把那个1置换为0。

4. 最高位的 1 → 置 0 该位

else if ((num & (1 << i)) && (i == highest_bit)) {

res = num & ~(1 << i);

break;

}

[111] & ~[100] --> [111] & [011] --> [011]

时间效率显著提升,但是空间效率依旧很低。

方法三:

解题思路:

知识补充:

1.lowbit 函数

lowbit(a) = a & -a,作用是找到 a 的二进制中最低位的 1 对应的数值(比如 a=6=110 → lowbit(6)=2;a=8=1000 → lowbit(8)=8)

2.补码规则

计算机中负数用补码表示,-a = ~a + 1(所以 ~a = -a - 1,这是理解 t & -t 的关键)

完整代码:

class Solution {

public:

vectorminBitwiseArray(vector& nums) {

for (int& x : nums) { // 引用:直接修改原数组,节省空间

if (x == 2) {

x = -1; // 2是偶质数,无满足条件的res

} else {

int t = ~x; // 对x按位取反

x ^= (t & -t) >> 1; // 核心:一步算出最小res

}

}

return nums;

}

};

解析:

int t = ~x; // 对x按位取反

x ^= (t & -t) >> 1; // 核心:一步算出最小res

子步骤 1:

t = ~ x --> 取反所有x的二进制位。

举例:5[101] --> [010]

子步骤2:

t & -t,作用是找到 a 的二进制中最低位的 1 对应的数值,在这里因为t = ~x,代表找到x的最低位0对应的数值

举例:[010] & [110] == [010]

子步骤3:

(t & -t) >> 1 → 找到需要翻转的 1 的位置

把 “最低位 0” 对应的数值右移 1 位,就是 x 中需要从 1 翻转为 0 的位

举例: [010] >> 1 ==[001]

子步骤4:

x^= … ,翻转指定为的1为0。

异或(^=)的特性:和 1 异或会翻转位(1→0,0→1),和 0 异或不变。

举例:[101] ^ [001] ==[100]

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧