【LeetCode】461. Hamming Distance

问题描述

问题链接:https://leetcode.com/problems/hamming-distance/#/description

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

Given 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.
Subscribe to see which companies asked this question.

我的代码

探索过程

一开始想的是两个数异或,然后把异或出来的结果转成二进制的字符串(”01001”这样),然后数1的个数。后来觉得用位操作肯定更快啊,就准备这样写:

public class Solution {
    public int hammingDistance(int x, int y) {
         // 思路弄一个int当掩码,
         // 从0x01到0x80
         // 每次左移一位去跟两个数与,如果结果不一样就加1
        int mask = 0x01;
        int count = 0;
        while (mask <= 0x80){
            if((x & mask) != (y & mask)){
                count ++;
            }
            mask <<= 1;
        }
        return count;
    }
}

我把一个16进制记成16位了,当然结果是没有通过。我仔细想了想,觉得知道问题在于位数了,所有改成了下面这样:

public class Solution {
    public int hammingDistance(int x, int y) {
         // 思路弄一个int当掩码,
         // 从0x00000001到0x80000000
         // 每次左移一位去跟两个数与,如果结果不一样就加1
        int mask = 0x00000001;
        int count = 0;
        while (mask <= 0x80000000){
            if((x & mask) != (y & mask)){
                count ++;
            }
            mask <<= 1;
        }
        return count;
    }
}

这样了以后连1,4的测试用例都过不了了,我觉得很奇怪啊,凭什么啊。我怀疑是那个提交的系统出错了,就在IDE上试了试(嗯,没错,作弊了),确实是过不了。用IDE调试,发现0x80000000就是个负数。终于想起来符号位的事。改了过来。

通过的代码

public class Solution {
    public int hammingDistance(int x, int y) {
         // 思路弄一个int当掩码,
         // 从0x00000001到0x40000000(到这是因为再左移一位就成负数了),
         // 每次左移一位去跟两个数与,如果结果不一样就加1
        int mask = 0x00000001;
        int count = 0;
        while (mask <= 0x40000000 && mask > 0){
            if((x & mask) != (y & mask)){
                count ++;
            }
            mask <<= 1;
        }
        return count;
    }
}

修改以后过了。可是运行速度只超过了5%的Java代码,再去评论区好好学习一个。

讨论区

来,见识一下大神们的风采。只能说大写的服!

Java 1 Line Solution :D

https://discuss.leetcode.com/topic/72093/java-1-line-solution-d

public class Solution {
    public int hammingDistance(int x, int y) {
        return Integer.bitCount(x ^ y);
    }
}

各种bitCount,来好好学习一个。https://tech.liuchao.me/2016/11/count-bits-of-integer/

the native way
int bitCount(int n) {
    int count = 0;

    for (int i = 0; i < 32; i++) {
        count += n & 1;
        n >>= 1;
    }

    return count;
}
Brian Kernighan’s way

n & (n – 1) will clear the last significant bit set, e.g. n = 112

     n     | 1 1 1 0 0 0 0
   n - 1   | 1 1 0 1 1 1 1
n &= n - 1 | 1 1 0 0 0 0 0
---------------------------
     n     | 1 1 0 0 0 0 0
   n - 1   | 1 0 1 1 1 1 1
n &= n - 1 | 1 0 0 0 0 0 0
---------------------------
     n     | 1 0 0 0 0 0 0
   n - 1   | 0 1 1 1 1 1 1
n &= n - 1 | 0 0 0 0 0 0 0

Hence loop will go through as many iterations as there are set bits, and when n becomes zero, count is exactly the answer.

int bitCount(int n) {
    int count = 0;
    while (n != 0) {
        n &= n - 1;
        count++;
    }
    return count;
}

Java 3-Line Solution

https://discuss.leetcode.com/topic/72089/java-3-line-solution

public int hammingDistance(int x, int y) {
    int xor = x ^ y, count = 0;
    for (int i=0;i<32;i++) count += (xor >> i) & 1;
    return count;
}

嗯,LeetCode值得好好刷刷。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值