461. Hamming Distance [LeetCode]
来源:互联网 发布:未来计价软件视频 编辑:程序博客网 时间:2024/05/19 07:44
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 = 4Output: 2Explanation:1 (0 0 0 1)4 (0 1 0 0) ↑ ↑The above arrows point to positions where the corresponding bits are different.
这道题让我求两个数字之间的汉明距离,题目中解释的很清楚了,两个数字之间的汉明距离就是其二进制数对应位不同的个数。
那么最直接了当的做法就是按位分别取出两个数对应位上的数并异或,我们知道异或的性质上相同的为0,不同的为1,我们只要把为1的情况累加起来就是汉明距离了,参见代码如下:
我们可以对上面的代码进行优化,我们可以一开始直接将两个数字异或起来,然后我们遍历异或结果的每一位,统计为1的个数,也能达到同样的效果,参见代码如下:
上面的遍历每一位的方法并不高效,还可以进一步优化,假如数为c, c & (c - 1)可以快速地移除最右边的bit 1, 一直循环到c为0, 总的循环数就是c中bit 1的个数。参见代码如下:
我们再来看一种递归的写法,非常的简洁,递归终止的条件是当两个数异或为0时,表明此时两个数完全相同,我们返回0,否则我们返回异或和对2取余加上对x/2和y/2调用递归的结果。异或和对2取余相当于检查最低位是否相同,而对x/2和y/2调用递归相当于将x和y分别向右移动一位,这样每一位都可以比较到,也能得到正确结果,参见代码如下:
0 0
- LeetCode 461. Hamming Distance
- 【Leetcode】461. Hamming Distance
- LeetCode 461. Hamming Distance
- LeetCode - 461. Hamming Distance
- LeetCode-461. Hamming Distance
- 【LeetCode】 461. Hamming Distance
- Leetcode-461. Hamming Distance
- Leetcode 461. Hamming Distance
- LeetCode--461. Hamming Distance
- 461. Hamming Distance [LeetCode]
- [LeetCode]461. Hamming Distance
- leetcode 461. Hamming Distance
- Leetcode 461. Hamming Distance
- LeetCode 461. Hamming Distance
- 【LeetCode】461. Hamming Distance
- LeetCode-461. Hamming Distance
- LeetCode | 461. Hamming Distance
- [LeetCode]461.Hamming Distance
- 哈希表原理
- Markdown-01文本格式化(学习笔记)
- Error:(22, 0) Could not find method android() for argument
- C++建立查找删除节点二叉排序树
- 欢迎使用CSDN-markdown编辑器
- 461. Hamming Distance [LeetCode]
- 每天一个linux命令(1)
- nyoj26_孪生素数(筛选法求素数)
- ZooKeeper的Java Api初探
- 隐公元年
- 通俗理解卷积神经网络
- Java内存模型 读笔
- 双tag线段树
- Shadowsocks初识及VPN和VPS区别