【Leetcode461】Hamming Distance

不知道爲什麼先前這種通不過 

class Solution {
public:
    int hammingDistance(int x, int y) {
        int answer = 0;
        int tmp = x^y;
        int count = 31;
        while(count--){
            // if(x&1 != y&1){
            //     answer += 1;
            // }
            // x >>= 1;
            // y >>= 1;
            if(tmp&1 == 1){
                answer += 1;
            }
            tmp >>= 1;
        }
        return answer;
    }
};

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------知道錯誤的原因了,!=優先級是7,按位&優先級是8,所以這樣寫實際上是先做了!=後做了&

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章