位运算(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 ...
随机推荐
- [转]关于TDD、BDD和DDD的一些看法
在实际的项目中,我们可能随时面对各种不同的需求,它的各个方面的要素决定了我们所采用的开发模式. 比如,它的复杂度如何?所有的需求是否足够清晰?开发人员对相关的业务是否足够了解?项目的工期是否合理?种种 ...
- CLH同步队列
原文链接:https://blog.csdn.net/chenssy/article/details/60781148 AQS内部维护着一个FIFO队列,该队列就是CLH同步队列. CLH同步队列是一 ...
- EOS 增发与生产者的奖励制度
EOS每年增发1%的机制在系统合约中,其实说每年增发1%只是一年的总数,其实是只要在出块,EOS就在增发的路途中,下面分析一下增发的代码. 其实增发的1%的都是分给所有区块生产者的,只要出块了或者获得 ...
- [HAOI2012]音量调节 BZOJ2748 dp
题目描述 一个吉他手准备参加一场演出.他不喜欢在演出时始终使用同一个音量,所以他决定每一首歌之前他都需要改变一次音量.在演出开始之前,他已经做好一个列表,里面写着每首歌开始之前他想要改变的音量是多少. ...
- libxml2 安装及使用
https://gitlab.gnome.org/GNOME/libxml2/ ftp://xmlsoft.org/libxml2/libxml2-2.9.1.tar.gz /configuremak ...
- [題解](最小生成樹)luogu_P2916安慰奶牛
可以發現每個點經過次數恰好等於這個點的度數,所以把點權下放邊權,跑最小生成樹,原來邊權乘二在加上兩端點權,答案再加一遍起點最小點權 #include<bits/stdc++.h> #def ...
- spring boot +Thymeleaf+mybatis 集成通用PageHelper,做分页
controller: /** * 分页查询用户 * @param request * @param response * @return * @throws Exception */ @ ...
- Navicat12破解
Navicat12破解 http://www.sdbeta.com/xiazai/2017/0818/209765.html
- 简单理解php的socket连接
socket建立套接的过程图: 首先了解socket 几个主要函数: socket的关键函数1: socket_create($net参数1,$stream参数2,$protocol参数3) 作用:创 ...
- pandas学习2(基础操作)