LeetCode 461. Hamming Distance (C++)
题目:
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 < 2^31.
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.
分析:
将两个数的二进制进行比较,不同的计数加一,最后返回总数。
由于1&1 = 1,1&0 = 0,可以分别将x,y和1做与运算得到x,y的最后一位的值,再做异或运算,然后再将x,y右移一位。因为题里限定了x,y大小,所以只32次循环就够了。
程序:
class Solution {
public:
int hammingDistance(int x, int y) {
int nums = ;
for (int i = ; i < ; i++){
if ((x & ) ^ (y & ))
nums++;
x = x >> ;
y = y >> ;
}
return nums;
}
};
LeetCode 461. Hamming Distance (C++)的更多相关文章
- LeetCode:461. Hamming Distance
package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...
- Leetcode#461. Hamming Distance(汉明距离)
题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...
- LeetCode 461. Hamming Distance (汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] 461. Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 4. leetcode 461. Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- LeetCode 461 Hamming Distance 解题报告
题目要求 The Hamming distance between two integers is the number of positions at which the corresponding ...
- [LeetCode] 461. Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...
- Leetcode - 461. Hamming Distance n&=(n-1) (C++)
1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法.用了n&a ...
- [Leetcode/Javascript] 461.Hamming Distance
[Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...
随机推荐
- PATtest1.3:最大子列和
题目源于:https://pintia.cn/problem-sets/16/problems/663 题目要求:输入一个数列,求其最大子列和. 问题反馈:1.部分C++代码不是很熟练 2.没有仔细读 ...
- NYOJ 36 LCS(最长公共子序列)
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB ...
- 郑重推荐开源CANopen协议栈CANFestival(LGPL许可)!!!!!!!!
郑重推荐开源CANopen协议栈CANFestival(LGPL许可)!!!!!!!!(这条文章已经被阅读了 次) 时间:2010/03/04 06:47am 来源:winshton [这个贴子最后由 ...
- [Phonegap+Sencha Touch] 移动开发29 安卓navigator.camera.getPicture得到图片的真实路径
原文地址:http://blog.csdn.net/lovelyelfpop/article/details/38313021 phonegap的拍照插件选择图库中的图片.代码例如以下: naviga ...
- 实现点击到底部、顶部、指定div功能
顶部: $(".back_top").click(function () { scrollTo(0, 0); }); function goTop() { $('html, bod ...
- day 86 Vue学习之五DIY脚手架、webpack使用、vue-cli的使用、element-ui
本节目录 一 vue获取原生DOM的方式 二 DIY脚手架 三 vue-cli脚手架的使用 四 webpack创建项目的玩法 五 element-ui的使用 六 xxx 七 xxx 八 xxx 一 ...
- Scala_类和对象
类是对象的抽象,而对象是类的具体实例.类是抽象的,不占用内存,而对象是具体的,占用存储空间. import scala.beans.BeanProperty class ChecksumAccumul ...
- WPF触控方面的技术点
一.基本的触控事件(原始触控) 二.复杂触控事件(操作)
- 洛谷 P4026 [SHOI2008]循环的债务
水水的dp 设f[i][a][b]表示交换完前i种面值的钞票,第一个人有a元,第二个人有b元的最小代价 直接转移就行了 需要注意的是算的式子 第1个人\(\Delta A\),第二个人\(\Delta ...
- cogs1772 [国家集训队2010]小Z的袜子
沉迷于文化的我N年没更blog了...(\(N \in (0,1)\)) 然后回到机房就沉迷于 \(generals.io\) 无法自拔...QAQ 然后想打一遍splay(然后是LCT),然后放弃了 ...