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 ≤ x
, y
< 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.
public class Solution {
public int hammingDistance(int x, int y) {
int count = ;
for (int i = ; i <= ; i++) {
int value1 = (x >> i) & ;
int value2 = (y >> i) & ;
count += (value1 ^ value2);
}
return count;
}
}
Hamming Distance的更多相关文章
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 461. Hamming Distance and 477. Total Hamming Distance in Python
题目: The Hamming distance between two integers is the number of positions at which the corresponding ...
- LeetCode Total Hamming Distance
原题链接在这里:https://leetcode.com/problems/total-hamming-distance/ 题目: The Hamming distance between two i ...
- LeetCode Hamming Distance
原题链接在这里:https://leetcode.com/problems/hamming-distance/ 题目: The Hamming distance between two integer ...
- LeetCode:461. Hamming Distance
package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...
- 64. 海明距离(Hamming Distance)
[本文链接] http://www.cnblogs.com/hellogiser/p/hamming-distance.html [介绍] 在信息领域,两个长度相等的字符串的海明距离是在相同位置上不同 ...
- hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
随机推荐
- .net core 源码解析-mvc route的注册,激活,调用流程(三)
.net core mvc route的注册,激活,调用流程 mvc的入口是route,当前请求的url匹配到合适的route之后,mvc根据route所指定的controller和action激活c ...
- 阿里云VPS服务器,ROS内网穿透
Aliyun Windows Server 2008 R2中建立vpn服务器,ros中使用pptp拨号连接 2.在Aliyun服务器中,修改hosts,将内网分配的ip映射到指定的域名,在Aliyun ...
- a版本十日冲刺总汇
DAY ONE: http://www.cnblogs.com/aruba/p/6041243.html 2016-11-08 DAY TWO: http://www.cnblogs.com/arub ...
- json数组去重
//名字去重 Map<String,Integer> map=new HashMap<String,Integer>(); for(int i=0;i<jows.size ...
- JavaScript备忘录
提取字符串substring(start,end)substr(start [, length ])JavaScript substr() 方法 --
- 浅谈:javascript的面向对象编程之具体实现
下面的javascript代码都是需要使用jQuery插件来做的.希望大家可以搭建好工作环境 首先我们来做一个练习:在一个删除的超链接中添加一个提示信息,提示是否确认删除. 一般情况下我们都会这么做 ...
- PHPCMS调用点击量的方法
具体方法: 首先:需要在show.html 模板中首先加载jquery.min.js 这个js 其次:需要在要显示点击次数的位置写入: <span>点击:<span id=" ...
- UML大战需求分析--阅读笔记02
这次阅读了第三章--类图.本章主要讲解了类图的基本使用规则和一些使用的例子.类图是UML中非常重要的一部分,作用很大. 类图之间有五种关系:关联关系,聚合关系,组合关系,泛化关系,依赖关系.关联关系有 ...
- 解决Maven工程中报 Missing artifact jdk.tools:jdk.tools:
jdk.tools:jdk.tools是与JDK一起分发的一个JAR文件,可以如下方式加入到Maven项目中:<dependency> <groupId>jdk.tool ...
- angularjs-$http.post请求传递参数,后台Controller接受不到原因
现象回显 js文件 app.controller('indexCtrl', function($scope, $state, $http) { $scope.login = function() { ...