2220. Minimum Bit Flips to Convert Number
· 閱讀時間約 1 分鐘
class Solution {
public:
int minBitFlips(int start, int goal)
{
int n = start ^ goal;
int cnt = 0;
while (n)
{
n &= (n - 1);
cnt++;
}
return cnt;
}
};
- T: $O(number of bits)$
- S: $O(1)$