[LeetCode] 461. Hamming Distance

https://en.wikipedia.org/wiki/Hamming_distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x and y, calculate the Hamming distance.
Note:
0 ≤ xy < 231.

題目給出了 x, y 的大小定義, 大大降低題目難度.

思路:

1.用python 內建的string format 成帶0的二進為字串
2.比較 x, y 的二進為字串長度, 找出差距(overflow). 因為一個字串有 另一個沒有 所以compare一定不同. Overflow 的值一定是 hamming distance
3.用一個 for loop 掃一遍 並 比較 x, y
4.最後加上 overflow 並回傳




留言