返回介绍

solution / 2400-2499 / 2429.Minimize XOR / README_EN

发布于 2024-06-17 01:03:06 字数 6777 浏览 0 评论 0 收藏 0

2429. Minimize XOR

中文文档

Description

Given two positive integers num1 and num2, find the positive integer x such that:

  • x has the same number of set bits as num2, and
  • The value x XOR num1 is minimal.

Note that XOR is the bitwise XOR operation.

Return _the integer _x. The test cases are generated such that x is uniquely determined.

The number of set bits of an integer is the number of 1's in its binary representation.

 

Example 1:

Input: num1 = 3, num2 = 5
Output: 3
Explanation:
The binary representations of num1 and num2 are 0011 and 0101, respectively.
The integer 3 has the same number of set bits as num2, and the value 3 XOR 3 = 0 is minimal.

Example 2:

Input: num1 = 1, num2 = 12
Output: 3
Explanation:
The binary representations of num1 and num2 are 0001 and 1100, respectively.
The integer 3 has the same number of set bits as num2, and the value 3 XOR 1 = 2 is minimal.

 

Constraints:

  • 1 <= num1, num2 <= 109

Solutions

Solution 1: Greedy + Bit Manipulation

According to the problem description, we first calculate the number of set bits $cnt$ in $num2$, then enumerate each bit of $num1$ from high to low. If the bit is $1$, we set the corresponding bit in $x$ to $1$ and decrement $cnt$ by $1$, until $cnt$ is $0$. If $cnt$ is still not $0$ at this point, we start from the low bit and set each bit of $num1$ that is $0$ to $1$, and decrement $cnt$ by $1$, until $cnt$ is $0$.

The time complexity is $O(\log n)$, and the space complexity is $O(1)$. Here, $n$ is the maximum value of $num1$ and $num2$.

class Solution:
  def minimizeXor(self, num1: int, num2: int) -> int:
    cnt = num2.bit_count()
    x = 0
    for i in range(30, -1, -1):
      if num1 >> i & 1 and cnt:
        x |= 1 << i
        cnt -= 1
    for i in range(30):
      if num1 >> i & 1 ^ 1 and cnt:
        x |= 1 << i
        cnt -= 1
    return x
class Solution {
  public int minimizeXor(int num1, int num2) {
    int cnt = Integer.bitCount(num2);
    int x = 0;
    for (int i = 30; i >= 0 && cnt > 0; --i) {
      if ((num1 >> i & 1) == 1) {
        x |= 1 << i;
        --cnt;
      }
    }
    for (int i = 0; cnt > 0; ++i) {
      if ((num1 >> i & 1) == 0) {
        x |= 1 << i;
        --cnt;
      }
    }
    return x;
  }
}
class Solution {
public:
  int minimizeXor(int num1, int num2) {
    int cnt = __builtin_popcount(num2);
    int x = 0;
    for (int i = 30; ~i && cnt; --i) {
      if (num1 >> i & 1) {
        x |= 1 << i;
        --cnt;
      }
    }
    for (int i = 0; cnt; ++i) {
      if (num1 >> i & 1 ^ 1) {
        x |= 1 << i;
        --cnt;
      }
    }
    return x;
  }
};
func minimizeXor(num1 int, num2 int) int {
  cnt := bits.OnesCount(uint(num2))
  x := 0
  for i := 30; i >= 0 && cnt > 0; i-- {
    if num1>>i&1 == 1 {
      x |= 1 << i
      cnt--
    }
  }
  for i := 0; cnt > 0; i++ {
    if num1>>i&1 == 0 {
      x |= 1 << i
      cnt--
    }
  }
  return x
}
function minimizeXor(num1: number, num2: number): number {
  let cnt = 0;
  while (num2) {
    num2 &= num2 - 1;
    ++cnt;
  }
  let x = 0;
  for (let i = 30; i >= 0 && cnt > 0; --i) {
    if ((num1 >> i) & 1) {
      x |= 1 << i;
      --cnt;
    }
  }
  for (let i = 0; cnt > 0; ++i) {
    if (!((num1 >> i) & 1)) {
      x |= 1 << i;
      --cnt;
    }
  }
  return x;
}

Solution 2

class Solution:
  def minimizeXor(self, num1: int, num2: int) -> int:
    cnt1 = num1.bit_count()
    cnt2 = num2.bit_count()
    while cnt1 > cnt2:
      num1 &= num1 - 1
      cnt1 -= 1
    while cnt1 < cnt2:
      num1 |= num1 + 1
      cnt1 += 1
    return num1
class Solution {
  public int minimizeXor(int num1, int num2) {
    int cnt1 = Integer.bitCount(num1);
    int cnt2 = Integer.bitCount(num2);
    for (; cnt1 > cnt2; --cnt1) {
      num1 &= (num1 - 1);
    }
    for (; cnt1 < cnt2; ++cnt1) {
      num1 |= (num1 + 1);
    }
    return num1;
  }
}
class Solution {
public:
  int minimizeXor(int num1, int num2) {
    int cnt1 = __builtin_popcount(num1);
    int cnt2 = __builtin_popcount(num2);
    for (; cnt1 > cnt2; --cnt1) {
      num1 &= (num1 - 1);
    }
    for (; cnt1 < cnt2; ++cnt1) {
      num1 |= (num1 + 1);
    }
    return num1;
  }
};
func minimizeXor(num1 int, num2 int) int {
  cnt1 := bits.OnesCount(uint(num1))
  cnt2 := bits.OnesCount(uint(num2))
  for ; cnt1 > cnt2; cnt1-- {
    num1 &= (num1 - 1)
  }
  for ; cnt1 < cnt2; cnt1++ {
    num1 |= (num1 + 1)
  }
  return num1
}
function minimizeXor(num1: number, num2: number): number {
  let cnt1 = bitCount(num1);
  let cnt2 = bitCount(num2);
  for (; cnt1 > cnt2; --cnt1) {
    num1 &= num1 - 1;
  }
  for (; cnt1 < cnt2; ++cnt1) {
    num1 |= num1 + 1;
  }
  return num1;
}

function bitCount(i: number): number {
  i = i - ((i >>> 1) & 0x55555555);
  i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
  i = (i + (i >>> 4)) & 0x0f0f0f0f;
  i = i + (i >>> 8);
  i = i + (i >>> 16);
  return i & 0x3f;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文