Problem
Given two positive integers num1
and num2
, find the positive integer x
such that:
x
has the same number of set bits asnum2
, 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.
https://leetcode.com/problems/minimize-xor/
Example 1:
Input:
num1 = 3, num2 = 5
Output:3
Explanation:
The binary representations of num1 and num2 are0011
and0101
, respectively.
The integer 3 has the same number of set bits as num2, and the value3 XOR 3 = 0
is minimal.
Example 2:
Input:
num1 = 1, num2 = 12
Output:3
Explanation:
The binary representations of num1 and num2 are0001
and1100
, respectively.
The integer 3 has the same number of set bits as num2, and the value3 XOR 1 = 2
is minimal.
Constraints:
1 <= num1, num2 <= 10⁹
Test Cases
1 | class Solution: |
1 | import pytest |
Thoughts
先统计 num1 和 num2 各自二进制表示中的 1 的个数(可以用 191. Number of 1 Bits 里提到的各种方法,或者直接用语言自带的方法如 Python 中 int.bit_count
)。
如果它俩的二进制表示中 1 的个数一致,那么 num1 就是所求结果,它与自己的 XOR 结果为 0,是最小的。
如果 num1 的 1 的个数少一些,就需要把一些二进制位的 1 改成 0,为了让 XOR 的结果最小,显然应该先改低位的 1。
如果 num1 的 1 的个数多一些,就需要把一些二进制为的 0 改成 1,同理可知为了让 XOR 的结果最小,应该先改低位的 0。
时间复杂度 O(log num1 + log num2)
,空间复杂度 O(1)
。
Code
1 | class Solution: |