位运算(1)——Hamming Distance
https://leetcode.com/problems/hamming-distance/#/description
输入:两个整数x,y,且0 ≤ x, y < 231。
输出:x,y的二进制表示,不同的有几位。
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 = 0;
for(int i=0; i<32; i++) {
if((x & 1) != (y & 1)) {
count++;
x = x >> 1;
y = y >> 1;
} else {
x = x >> 1;
y = y >> 1;
}
}
return count;
}
}
位运算(1)——Hamming Distance的更多相关文章
- Leedcode算法专题训练(位运算)
https://www.cnblogs.com/findbetterme/p/10787118.html 看这个就完事了 1. 统计两个数的二进制表示有多少位不同 461. Hamming Dista ...
- 海明距离hamming distance
仔细阅读ORB的代码,发现有很多细节不是很明白,其中就有用暴力方式测试Keypoints的距离,用的是HammingLUT,上网查了才知道,hamming距离是相差位数.这样就好理解了. 我理解的Ha ...
- LeetCode解题中位运算的运用
位运算是我最近才开始重视的东西,因为在LeetCode上面刷题的时候发现很多题目使用位运算会快很多.位运算的使用包含着许多技巧(详细可以参考http://blog.csdn.net/zmazon/ar ...
- 461. Hamming Distance(leetcode)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- LeetCode编程训练 - 位运算(Bit Manipulation)
位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...
- LeetCode 461. Hamming Distance (C++)
题目: The Hamming distance between two integers is the number of positions at which the corresponding ...
- 477. Total Hamming Distance总的二进制距离
[抄题]: The Hamming distance between two integers is the number of positions at which the correspondin ...
- [Leetcode/Javascript] 461.Hamming Distance
[Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...
- 461. Hamming Distance(汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 算法与数据结构基础 - 位运算(Bit Manipulation)
位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...
随机推荐
- shell脚本编程的10个最佳实践
摘要:每一个在UNIX/Linux上工作的程序员可能都擅长shell脚本编程.对于那些处在shell脚本编程初级阶段的程序员来说,遵循一些恰当的做法可以帮助你更快.更好的学习这些编程技巧. 每一个在U ...
- Java框架之Java Bean
链接 知乎https://www.zhihu.com/question/19773379 总结 符合一定规范的编写的Java类,不是一种技术,而是一种规范.大家对于这种规范,总结了很多开发技巧,工具函 ...
- nodejs創建目錄命令mkdir失敗
Windows系統 學習nodejs創建目錄命令:mkdir var fs = require('fs'); fs.mkdir('./tmp/test',function (err) { if(err ...
- POJ3074 Sudoku 剪枝深(神?)搜
emm...挺秀的...挺神的? 每行,每列,每宫用一个二进制数表示选或没选的状态,刚开始设没选为1,然后更改状态的时候异或一下就好了: 这样可以通过lowbit取出每一个没有选过的数:(妙啊? 关于 ...
- pandas实例美国人口分析
- asp.net模板页实现类似jquery中document.ready
模板页先判断是否有方法DocumentReady,有的话就调用 1.模板页 <script type="text/javascript" language="jav ...
- JWT(JSON Web Token)原理简介
原文:http://www.fengchang.cc/post/114 参考了一下这篇文章:https://medium.com/vandium-software/5-easy-steps-to-un ...
- linux面试题:删除一个目录下的所有文件,但保留一个指定文件
面试题:删除一个目录下的所有文件,但保留一个指定文件 解答: 假设这个目录是/xx/,里面有file1,file2,file3..file10 十个文件 [root@oldboy xx]# touch ...
- Smarty保留变量信息
对php里边的超级全局数组变量信息的使用 例如:$_GET.$_POST.$_SESSION.$_COOKIE.$_REQUEST.$_SERVER.$_ENV.$GLOBALS.$_FILES.常量 ...
- 主席树-----动态开点,不hash
POJ - 2104 第k大 #include <cstdio> #include <cstdlib> #include <cstring> #include &l ...