461. Hamming Distance

该题目是求两个数之间的汉明距离,也就是两个数的二进制数中的位数不一样的个数。可以使用两个数组分别保存两个参数的二进制数,也可以使用异或操作,代码如下:

 1 class Solution {
 2     public int hammingDistance(int x, int y) {
 3         int i = x ^ y;
 4         int count=0;
 5         while (i != 0) {
 6             count++;
 7             i=(i-1)&i;  
 8         }
 9         return count;
10     }
11 }

 

END

 

posted @ 2018-05-02 10:33  sysu_kww  阅读(112)  评论(0编辑  收藏  举报