461. Hamming Distance
2016-12-18 12:40
274 查看
461. Hamming Distance
The
Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers
Note:
0 ≤
Example:
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.
说明:依次判断即可。
int hammingDistance(int x, int y) { int cnt=0; while(x||y) { if((x&1)^(y&1)) cnt++; x>>=1; y>>=1; } return cnt; }
相关文章推荐
- C++ 双链表的基本操作(详解)
- unity3d回调函数总结学习
- Checkbox复用导致错乱
- ccf 画图
- 如何在 Linux 中查找一个文件
- ubuntu16.10+cuda8.0+gtx1080
- BC-【Kblack loves flag】
- 软件测试基础题目
- Linux 中必须要了解的命令操作
- 获取和设置transform值
- Android 反编译初探 应用是如何被注入广告的
- opencv计算机视觉学习笔记八
- opencv计算机视觉学习笔记八
- opencv计算机视觉学习笔记七
- dtd详解
- 【软件周刊】要的就是速度,Angular 4.0.0 beta0 发布;JetBrains 带来全新 Go IDE — Gogland
- 使用 gpg 加密文件 - 通过 shell 或 php
- swift 命名,字符串
- opencv计算机视觉学习笔记七
- 学习java应该如何理解反射?