461. Hamming Distance

bitset

將數值轉換爲二進制

class Solution {
public:
    int hammingDistance(int x, int y) {
       string bin1 = bitset<32>(x).to_string();    
        string bin2 = bitset<32>(y).to_string();    
        int cnt = 0;
        for(int i = 0; i < bin1.size(); i++){
            if(bin1[i] != bin2[i]) cnt++;
        }
        return cnt;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章