原题链接在这里:https://leetcode.com/problems/hamming-distance/

题目:

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.

题解:

Bits different自然而然就想到了xor.

利用Number of 1 Bits的数xor 结果的bit 1个数.

或者用Integer.bitCount() function.

Time Complexity: O(1). Space: O(1).

AC Java:

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

跟上Total Hamming Distance.

LeetCode Hamming Distance的更多相关文章

  1. LeetCode——Hamming Distance

    LeetCode--Hamming Distance Question The Hamming distance between two integers is the number of posit ...

  2. [LeetCode] Hamming Distance 汉明距离

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

  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. C语言中字符串结束符'\0'

    转自:http://www.cnblogs.com/kaituorensheng/archive/2013/12/09/3464462.html 本质 '\0'就是8位的00000000,因为字符类型 ...

  2. html标签思维导图

  3. 逗号分割符--字段中含逗号等情况的解析方法Java实现

    最近在处理文本字符串时,没一行数据都是按照逗号分割的,每个字段值一般情况是带有双引号的,但是有的字段值里面还包含逗号,甚至有的字段就没有双引号,这个分割起来就有点麻烦了 下面说一下我解决方法,如果谁有 ...

  4. 疯狂房价"逼死"年轻人,别指望中国未来能出人才了

    社会高房价,杀死那个学者 --北京青年学者生存侧记 这一轮,房价又上涨了,只有更疯狂. 几年前,北京三环内的房价突破5万,世人惊呼:没几年,四环5万了,五环5万了:这一轮,北京城乡结合部,哪怕脏乱差之 ...

  5. BlockingQueue深入分析(转)

    1.BlockingQueue定义的常用方法如下   抛出异常 特殊值 阻塞 超时 插入 add(e) offer(e) put(e) offer(e,time,unit) 移除 remove() p ...

  6. Python中实现从目录中过滤出指定文件类型的文件

    摘自:http://www.jb51.net/article/60641.htm #!/usr/bin/env python import glob import os os.chdir(“./”) ...

  7. 【Cocos2d-x游戏开发】Cocos2d-x中的数据存储技术

    一.引言 数据存储和网络功能可以说是一款游戏中必不可少的功能,如果一款游戏不能保存进度那么它的可玩性必然大打折扣(试想一下,玩家辛辛苦苦玩了一整天的游戏,结果退出时告诉人家不能保存关卡信息,你明天还得 ...

  8. js 类型转换学习

    类型转换分为显示转换和隐式转换 参考http://www.cnblogs.com/mizzle/archive/2011/08/12/2135885.html 先事件显示的 通过手动进行类型转换,Ja ...

  9. 【BZOJ】3526: [Poi2014]Card

    题意 \(n(n \le 200000)\)张卡片,正反有两个数\(a[i], b[i]\).\(m(m \le 1000000)\)次操作,每次交换\(c[i].d[i]\)位置上的卡片.每一次操作 ...

  10. Linux文件系统扩容步骤

    1 扩容前检查 cat /etc/fstab df -h 在扩容之前请确认VG的Free大小,以及文件和文件系统是否达到系统限制 2 系统识别硬盘 #echo "- - -" &g ...