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(int start, int goal) {
    return __builtin_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 bin(start ^ goal).count('1')