Skip to content

2220. Minimum Bit Flips to Convert Number 👍

  • Time: $O(\log (\max(\texttt{start}, \texttt{goal})))$
  • Space: $O(1)$
1
2
3
4
5
6
class Solution {
 public:
  int minBitFlips(unsigned start, unsigned goal) {
    return popcount(start ^ goal);
  }
};
1
2
3
4
5
class Solution {
  public int minBitFlips(int start, int goal) {
    return Integer.bitCount(start ^ goal);
  }
}
1
2
3
class Solution:
  def minBitFlips(self, start: int, goal: int) -> int:
    return (start ^ goal).bit_count()