【LeetCode】461. Hamming Distance
2017-03-14 11:29
357 查看
问题描述
问题链接:https://leetcode.com/problems/hamming-distance/#/descriptionThe 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/to4000
pic/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 = 112n | 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-solutionpublic 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值得好好刷刷。
相关文章推荐
- LeetCode-461. Hamming Distance-位运算
- leetcode#461. Hamming Distance
- LeetCode 461. Hamming Distance
- 【Leetcode】461. Hamming Distance
- LeetCode: 461. Hamming Distance
- leetcode 461. Hamming Distance python实现
- leetcode 461. Hamming Distance
- Leetcode#461. Hamming Distance(异或)
- LeetCode 461. Hamming Distance
- leetcode 461. Hamming Distance
- [Leetcode] 461. Hamming Distance 解题报告
- LeetCode#461. Hamming Distance
- leetcode_461. Hamming Distance 计算汉明距离,按位异或运算,计算整数的二进制表示中1的个数 java
- LeetCode 461. Hamming Distance
- 【LeetCode】 461. Hamming Distance
- leetcode 461. Hamming Distance
- LeetCode 461. Hamming Distance(java)
- LeetCode 461. Hamming Distance 知识点复习之位运算
- [LeetCode]461. Hamming Distance(汉明距离)
- leetcode 461. Hamming Distance