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 ≤ xy < 231.

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.

两个数字之间的汉明距离是其二进制数对应位不同的个数,两个数异或,把为1的数量累加起来就是汉明距离。

Java:

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

Python:

class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
distance = 0
z = x ^ y
while z:
distance += 1
z &= z - 1
return distance

Python:

class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
return bin(x ^ y).count('1') 

C++:

class Solution {
public:
int hammingDistance(int x, int y) {
int res = 0;
for (int i = 0; i < 32; ++i) {
if ((x & (1 << i)) ^ (y & (1 << i))) {
++res;
}
}
return res;
}
};

C++:  

class Solution {
public:
int hammingDistance(int x, int y) {
int res = 0, exc = x ^ y;
while (exc) {
++res;
exc &= (exc - 1);
}
return res;
}
};

C++:

class Solution {
public:
int hammingDistance(int x, int y) {
if ((x ^ y) == 0) return 0;
return (x ^ y) % 2 + hammingDistance(x / 2, y / 2);
}
};

  

类似题目:  

[LeetCode] 191. Number of 1 Bits 二进制数1的个数

All LeetCode Questions List 题目汇总

[LeetCode] 461. Hamming Distance 汉明距离的更多相关文章

  1. Leetcode#461. Hamming Distance(汉明距离)

    题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...

  2. LeetCode:461. Hamming Distance

    package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...

  3. LeetCode 461. Hamming Distance (汉明距离)

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

  4. [LeetCode] 461. Hamming Distance(位操作)

    传送门 Description The Hamming distance between two integers is the number of positions at which the co ...

  5. 4. leetcode 461. Hamming Distance

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

  6. LeetCode 461 Hamming Distance 解题报告

    题目要求 The Hamming distance between two integers is the number of positions at which the corresponding ...

  7. LeetCode 461. Hamming Distance (C++)

    题目: The Hamming distance between two integers is the number of positions at which the corresponding ...

  8. 461. Hamming Distance(汉明距离)

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

  9. Leetcode - 461. Hamming Distance n&=(n-1) (C++)

    1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法.用了n&a ...

随机推荐

  1. 洛谷 P2722 总分题解

    题目描述 我们可以从几个种类中选取竞赛的题目,这里的一个"种类"是指一个竞赛题目的集合,解决集合中的题目需要相同多的时间并且能得到相同的分数.你的任务是写一个程序来告诉USACO的 ...

  2. 第一次使用Git(常用的dos命令整理)

    在使用git的过程中,有许多dos命令也要会用才行 Git 工具分类 命令行 bash.cmd.power shell GUI Git GUI.GitHub Desktop IDE 集成 Visual ...

  3. 关于srping的AOP事务管理问题,自定义切面是否导致事务控制失效

    applicationContext.xml: <!-- 方法调用时间记录 --> <bean id="methodExecuteTime" class=&quo ...

  4. lca:异象石(set+dfs序)

    题目:https://loj.ac/problem/10132 #include<bits/stdc++.h> using namespace std; ,N,k=,head[]; str ...

  5. HDU - 3311: Dig The Wells (斯坦纳树)

    题意:给你n个寺庙,m个村庄,p条路,现在你要在这n+m个位置中选出若干个位置打井,每个位置打井的费用会告诉你,同时p条路也有修建费用,现在每个寺庙都住着一个和尚,问你最小的费用让这n个和尚都能喝上水 ...

  6. Java LinkedList add vs push

    Java LinkedList add 是加在list尾部. LinkedList push 施加在list头部. 等同于addFirst.

  7. 关于异常System.ArgumentException

    什么是System.ArgumentException 当向方法提供的参数之一无效时引发的异常. 继承 Object Exception SystemException ArgumentExcepti ...

  8. 2019-8-26 LinkedHashMap 转 List [java.util.LinkedHashMap cannot be cast to com.zq.dataservice.bean.Index]

    java.util.LinkedHashMap cannot be cast to com.zq.dataservice.bean.Index 上述错误是在做一个趋势投资demo时遇到的. 说的是链式 ...

  9. 开源项目 11 Ionic Zip

    using Ionic.Zip; using System; using System.Collections.Generic; using System.IO; using System.Linq; ...

  10. [USACO09DEC] Dizzy Cows 拓扑序

    [USACO09DEC] Dizzy Cows 拓扑序 先对有向边跑拓扑排序,记录下每个点拓扑序,为了使最后的图不存在环,加入的\(p2\)条无向边\(u,v\)必须满足\(u\)拓扑序小于\(v\) ...