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. Google I/O 2013 – Volley: Easy, Fast Networking for Android

    1.什么是volley          Volley是Ficus Kirpatrick在Gooogle I/O 2013发布的一个处理和缓存网络请求的库,能使网络通信更快,更简单,更健壮.Volle ...

  2. Lisp语言简介

    摘自维基百科,原链接为:http://zh.wikipedia.org/zh/LISP 因为Clojure是Lisp的一种的方言,所以我们可以先来了解一下Lisp这个比较小众的编程到底是什么~ --- ...

  3. 160808、Java的不同版本:J2SE、J2EE、J2ME的区别

    来源:微学苑 在Java中,同一个类中的多个方法可以有相同的名字,只要它们的参数列表不同就可以,这被称为方法重载(method overloading). 参数列表又叫参数签名,包括参数的类型.参数的 ...

  4. C#自动给文章关键字加链接实现代码

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  5. 小程序wxParse插件的使用

    微信小程序输出html内容数据插件wxParse,可以把带html标签的数据输出为微信小程序正常显示的格式,wxParse插件带有演示,也有使用文档说明. 下载地址:https://github.co ...

  6. 2015-03-22——js常用的String方法

    String string.charAt(pos);  //返回字符串中pos位置处的字符.如果pos小于0或大于等于string.length返回空字符串.模拟实现:Function.prototy ...

  7. 2015-03-18——mongodb的简单配置

    参考网址:http://www.cnblogs.com/mecity/archive/2011/06/11/2078527.html#3060056 mongod 数据库启动程序 mongo 数据库操 ...

  8. golang的多协程实践

    go语言以优异的并发特性而闻名,刚好手上有个小项目比较适合. 项目背景: 公司播控平台的数据存储包括MySQL和ElasticSearch(ES)两个部分,编辑.运营的数据首先保存在MySQL中,为了 ...

  9. blogCMS整理

    一.在urls中写路由 二.返回登录页面(login.html中写前端代码) - username(用户名) - password(密码) - validCode(验证码) -submit(提交按钮) ...

  10. Keras实践:实现非线性回归

    Keras实践:实现非线性回归 代码 import os os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE" import ke ...