java.lang.Object
g2201_2300.s2220_minimum_bit_flips_to_convert_number.Solution

public class Solution extends Object
2220 - Minimum Bit Flips to Convert Number.<p>Easy</p> <p>A <strong>bit flip</strong> of a number <code>x</code> is choosing a bit in the binary representation of <code>x</code> and <strong>flipping</strong> it from either <code>0</code> to <code>1</code> or <code>1</code> to <code>0</code>.</p> <ul> <li>For example, for <code>x = 7</code>, the binary representation is <code>111</code> and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get <code>110</code>, flip the second bit from the right to get <code>101</code>, flip the fifth bit from the right (a leading zero) to get <code>10111</code>, etc.</li> </ul> <p>Given two integers <code>start</code> and <code>goal</code>, return <em>the <strong>minimum</strong> number of <strong>bit flips</strong> to convert</em> <code>start</code> <em>to</em> <code>goal</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> start = 10, goal = 7</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:</p> <ul> <li> <p>Flip the first bit from the right: 1010 -> 1011.</p> </li> <li> <p>Flip the third bit from the right: 1011 -> 1111.</p> </li> <li> <p>Flip the fourth bit from the right: 1111 -> 0111.</p> </li> </ul> <p>It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> start = 3, goal = 4</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:</p> <ul> <li> <p>Flip the first bit from the right: 011 -> 010.</p> </li> <li> <p>Flip the second bit from the right: 010 -> 000.</p> </li> <li> <p>Flip the third bit from the right: 000 -> 100.</p> </li> </ul> <p>It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 <= start, goal <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minBitFlips

      public int minBitFlips(int start, int goal)