461. 汉明距离

两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。

给出两个整数 x 和 y,计算它们之间的汉明距离。

注意:

0 ≤ x, y < 231.

示例:

输入: x = 1, y = 4

输出: 2

解释:

1 (0 0 0 1)

4 (0 1 0 0)

↑ ↑

上面的箭头指出了对应二进制位不同的位置。

class Solution {
public int hammingDistance(int x, int y) {
int z = x ^ y;
int sum = 0;
while (z!=0){
sum += z & 1;
z = z>>1;
}
return sum;
}
}

Java实现 LeetCode 461 汉明距离的更多相关文章

  1. LeetCode 461 汉明距离/LintCode 365 统计二进制中1的个数

    LeetCode 461. 汉明距离 or LintCode 365. 二进制中有多少个1 题目一:LeetCode 461. 汉明距离 LeetCode 461.明距离(Hamming Distan ...

  2. leetCode:461 汉明距离

    汉明距离 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 思路: 当看到"对应二进制位不同的位置的数目"这 ...

  3. Leetcode 461.汉明距离 By Python

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

  4. Java实现 LeetCode 477 汉明距离总和

    477. 汉明距离总和 两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量. 计算一个数组中,任意两个数之间汉明距离的总和. 示例: 输入: 4, 14, 2 输出: 6 解释: 在二进 ...

  5. 力扣Leetcode 461. 汉明距离

    给你一个数组 arr ,请你将每个元素用它右边最大的元素替换,如果是最后一个元素,用 -1 替换. 完成所有替换操作后,请你返回这个数组. 示例: 输入:arr = [17,18,5,4,6,1] 输 ...

  6. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  7. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  8. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  9. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

随机推荐

  1. 仿真FFT(quartus安装)

    软件下载:http://dl.altera.com/13.1/?edition=subscription 安装步骤: 接下来,仿真FFT: http://www.openhw.org/article/ ...

  2. python第二课list基本命令

    列表: stus= 'ada,dsfas.,saf,sdfas,saf'  #字符串new_stus = ['段','加','linux','123','数组']   #列表 取值方便#列表,数组,l ...

  3. CentOS 7搭建Zookeeper和Kafka集群

    环境 CentOS 7.4 Zookeeper-3.6.1 Kafka_2.13-2.4.1 Kafka-manager-2.0.0.2 本次安装的软件全部在 /home/javateam 目录下. ...

  4. 接口testing简介

    一.基础介绍 1.什么是接口 我们常说的接口一般指2种1)API:应用程序编程接口 2)GUI:图形用户界面(接口) 这里我们主要说API——接口测试   2.接口测试的目的 测试接口的正确性和稳定性 ...

  5. helm使用

    helm 0. helm安装 基本上到github上面,下载二进制就行.mac的话用brew安装. https://github.com/helm/helm brew: brew install ku ...

  6. iptables做nat网络地址转换

    iptables做nat网络地址转换. 0. 权威文档 http://www.netfilter.org/documentation/HOWTO/NAT-HOWTO-6.html e文好的直接跳过本文 ...

  7. python之Linux(Ubuntu)系统安装Python

    Linux 系统是为编程而生的,因此绝大多数的 Linux 发行版(Ubuntu.CentOS 等)都默认自带了 Python.有的 Linux 发行版甚至还会自带两个版本的 Python,例如最新版 ...

  8. js时间戳转为日期格式的方法

    Date.prototype.Format = function(fmt){ var o = { "M+" : this.getMonth()+1, //月份 "d+&q ...

  9. js 获取table tr td内的select 和input text

    $("#TableList tr").each(function () {                //for (var i = 1; i <= AM_index; i ...

  10. F. Dominant Indices

    题意:求每个点的子树中哪一层节点数最多,如果有节点数最多不唯一,取层数最小的. 题解:dus on tree 基本想法是对每一个节点都构建一个deep数组,然后从底向上更新过来,但是这样空间复杂度和时 ...