close

我的第一題leetcode,原先是解別題,但不太懂如何輸出

挑一題看起來比較簡單的來嘗試

The Hamming distance between two integers is the number of positions at which the corresponding bits are differents.

Givent two integers x and y, calculate the Hamming distance.

Note:

0 <= x, y < 2^31

Example:

Input: x = 1, y = 4

Output: 2

Explanation:
1   (0 0 0 1)
4   (0 1 0 0)
       ↑   ↑

The above arrows point to positions where the corresponding bits are different.

題意是輸入兩個十進位數字換為二進位,並指出他們同位數不同的數字有幾個

想法:

先想想,如何將十進位換成二進位

短除法的概念,除以二看除不除的盡?若有餘數則此位數為1,若無則為0

最後再做比較的動作

Accept

這也是我第一篇解題紀錄,希望能藉由此提升自己敘述解題的能力

arrow
arrow
    文章標籤
    解題
    全站熱搜
    創作者介紹
    創作者 Davis 的頭像
    Davis

    Epoch

    Davis 發表在 痞客邦 留言(0) 人氣()