汉明距离
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
给出两个整数 x 和 y,计算它们之间的汉明距离。
注意:
0 ≤ x, y < 231.
示例:
输入: x = 1, y = 4
 
输出: 2
 
解释:
1 (0 0 0 1)
4 (0 1 0 0)
        ↑   ↑
 
上面的箭头指出了对应二进制位不同的位置。
 
 
^
如果存在于其中一个操作数中但不同时存在于两个操作数中,二进制异或运算符复制一位到结果中。

思路:利用了异或的性质^   两个数字的二进制字符,比如   01001^10010=11011

异或^得到的结果中含有多少个1,就表示他的汉明距离。和上一道题:位1的个数 类似https://www.cnblogs.com/patatoforsyj/p/9475383.html

代码如下:

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

leetcode-汉明距离的更多相关文章

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

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

  2. [LeetCode] Hamming Distance 汉明距离

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

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

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

  4. 【LeetCode】汉明距离(Hamming Distance)

    这道题是LeetCode里的第461道题. 题目描述: 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x,  ...

  5. [LeetCode] 477. Total 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 bits ...

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

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

  8. leetCode:461 汉明距离

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

  9. 从0开始的LeetCode生活—461-Hamming Distance(汉明距离)

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

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

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

随机推荐

  1. Gradle Goodness: Check Task Dependencies With a Dry Run

    We can run a Gradle build without any of the task actions being executed. This is a so-called dry ru ...

  2. 查询sqlserver 表结构呀

    SQL Server里查询表结构命令 对于SQL Server数据库有两种方法查询表结构 第一种方法 sp_help Accounts_Users     其中Accounts_Users 表示表名 ...

  3. java判断类型

    判断是否String:str.getClass().getName().equals("java.lang.String") 判断是否在且不为空:Object.hasKey(&qu ...

  4. 自动化运维工具Ansible实战(四)常用模块

    转载链接:http://blog.51cto.com/liqingbiao/1962609   Ansible模块按功能分为:云模块.集群模块. 命令模块.数据库模块.文件模块.资产模块.消息模块.监 ...

  5. 使用第三方库iOS-ECharts做柱状图的心得

    最近的项目里面用到了饼图和条形统计图,饼图用的是PNChart来做的,这个库感觉用起来也简单,但是做条形统计图的时候就特别蛋疼(不知道是不是我姿势没对),反正就是各种问题,然后就想到换一种框架,最后选 ...

  6. Ubuntu下Zabbix结合percona监控mysql数据

    按道理来说zabbix就自带的MySQL插件来监控mysql数据库,但是你会发现,自带的mysql监控项是很少的,根本满足不了公司的需求.由于它本身自带的模板太过简单了,所以需要做更详细的监控,而pe ...

  7. wordpress | 网站访问速度优化方案(Avada)

    一.谷歌字体 原因: Wordpress系统默认使用谷歌字体,在国内谷歌域名被屏蔽,所以导致操作反应慢. 解决方法: 对于后台:找到Wordpress这个文件 /wp-includes/script- ...

  8. Kaggle比赛总结

    做完 Kaggle 比赛已经快五个月了,今天来总结一下,为秋招做个准备. 题目要求:根据主办方提供的超过 4 天约 2 亿次的点击数据,建立预测模型预测用户是否会在点击移动应用广告后下载应用程序. 数 ...

  9. PHP Mysql字符集utf8mb4支持Emoji表情

    项目开发中经常会遇到用户在评论或者发表文章的时候会打一些表情在里面,如果我们在开发中不去做一些处理的话,表情会出不来的,甚至是报错,下面简单介绍处理方式.原文地址:小时刻个人博客:http://sma ...

  10. java中子类会继承父类的构造方法吗?

    参考: https://blog.csdn.net/wangyl_gain/article/details/49366505