位操作-leetcode 461 Hamming Distance

原題鏈接:Hamming Distance


題解:

class Solution {
public:
    int hammingDistance(int x, int y) {
        /*
        	Time Complexity:O(1)
               Space Complexity:O(1)
        */
        x^=y;
        int count=0;
        while(x){
            count+=x&1;
            x=x>>1;
        }
        return count;
    }
};


發佈了61 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章