Class Solution
-
- All Implemented Interfaces:
public final class Solution2429 - Minimize XOR.
Medium
Given two positive integers
num1andnum2, find the positive integerxsuch that:xhas the same number of set bits asnum2, andThe value
x XOR num1is minimal.
Note that
XORis the bitwise XOR operation.Return the integer
x. The test cases are generated such thatxis 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 = 0is 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 = 2is minimal.Constraints:
<code>1 <= num1, num2 <= 10<sup>9</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerminimizeXor(Integer num1, Integer num2)-
-
Method Detail
-
minimizeXor
final Integer minimizeXor(Integer num1, Integer num2)
-
-
-
-