461. Hamming Distance 难度:easy
2017-01-08 20:53
267 查看
题目:
The Hamming distance between two integers is the number of positions at which the corresponding
bits are different.
Given two integers
calculate the Hamming distance.
Note:
0 ≤
231.
Example:
程序:
class Solution {
public:
int hammingDistance(int x, int y) {
int temp = x ^ y;
int count = 0;
while(temp)
{
count += (temp % 2);
temp /= 2;
}
return count;
}
};
The Hamming distance between two integers is the number of positions at which the corresponding
bits are different.
Given two integers
xand
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.
程序:
class Solution {
public:
int hammingDistance(int x, int y) {
int temp = x ^ y;
int count = 0;
while(temp)
{
count += (temp % 2);
temp /= 2;
}
return count;
}
};
相关文章推荐
- 同时安装python2.x和python3.x用pip
- Swift-内存管理(初级)
- 01套按字及http基础知识
- 正则表达式从入门到以为精通再到每次使用查一遍
- 用jQuery实现优酷首页轮播图
- 无题
- 一些投资理财渠道(仅供参考)
- 一看就懂的数据库范式介绍(1NF—5NF)
- php 的九大全局变量$_GET,$_POST,$_SERVER
- BZOJ4676 Xor-Mul棋盘/BZOJ3254 Xor-Mul Chessboard
- CentOS6 网络管理之网卡配置及简单路由设置
- ArcEngine中使用上下左右键移动地图
- C# GUID的使用
- Android textview 属性大全
- tomcat出现403问题
- JAVA 多线程
- 众数问题
- 翻译:结合卫星图像和机器学习预测贫穷
- OpenGL 入门13
- Swift-析构方法与内存管理(初级)