LeetCode——Hamming Distance

Question

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.

具体实现

class Solution {
public:
int hammingDistance(int x, int y) {
int tmp = x ^ y;
int res = 0;
while (tmp > 0) {
if (tmp % 2 != 0)
res++;
tmp = tmp >> 1;
}
return res;
}
};

LeetCode——Hamming Distance的更多相关文章

  1. [LeetCode] Hamming Distance 汉明距离

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

  2. LeetCode Hamming Distance

    原题链接在这里:https://leetcode.com/problems/hamming-distance/ 题目: The Hamming distance between two integer ...

  3. [LeetCode] Total Hamming Distance 全部汉明距离

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

  4. LeetCode Total Hamming Distance

    原题链接在这里:https://leetcode.com/problems/total-hamming-distance/ 题目: The Hamming distance between two i ...

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

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

  6. [Leetcode/Javascript] 461.Hamming Distance

    [Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...

  7. [LeetCode] 477. Total Hamming Distance 全部汉明距离

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

  8. [LeetCode] 461. Hamming Distance 汉明距离

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

  9. 【LeetCode】461. Hamming Distance 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 方法一:异或 + 字符串分割 方法二: ...

随机推荐

  1. 让python pip使用国内镜像

    国内源: 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 h ...

  2. linux下安装oracle sqlplus以及imp、exp工具

    一.下载oracle 11g sqlplus软件 linux 64位操作系统,oracle安装包地址 http://www.oracle.com/technetwork/topics/linuxx86 ...

  3. iOS企业开发In House ipa发布流程

    这两天需要发布一个ipa放到网上供其他人安装,需要用到企业级开发者账号.在网上查了一下资料,感觉没有一个比较完善的流程,于是决定把整个流程写下来,供大家参考. 首先详细说明一下我们的目标,我们需要发布 ...

  4. :nth- 从1开始计数,其他如:eq()、 index()从0开始计数

    因为jQuery的实现:nth-是严格来自CSS规范,n值是“1-indexed”,也就是说,从1开始计数. 对于所有其他选择器表达式比如:eq() 或 :even ,jQuery遵循JavaScri ...

  5. 在Editplus中Dev C++配置C++的编译运行环境

    1.首先得下载安装DEV-cpp 2.打开Editplus编辑器,工具->配置自定义工具 3.具体配置 编译C:命令:D:\Dev-Cpp\MinGW64\bin\g++.exe参数:" ...

  6. 如何设置,使IntelliJ IDEA智能提示忽略大小写

  7. C#中对文件的操作小结

    1.建立一个文本文件 public class FileClass { public static void Main() { WriteToFile(); } static void WriteTo ...

  8. https://github.com/arut/nginx-rtmp-module.git

    https://github.com/arut/nginx-rtmp-module.git NGINX-based Media Streaming Server nginx-rtmp-module P ...

  9. Java 之综合练习

    // 练习一: 写出程序结果 interface A{} class B implements A { public String func() { return "func"; ...

  10. python内存泄露查找

    1 前言: 1.1 像Java程序一样,虽然Python本身也有垃圾回收的功能,但是同样也会产生内存泄漏的问题 1.2 在Python程序里,内存泄漏是由于一个长期持有的对象不断的往一个dict或者l ...