leetcode-461. Hamming Distance
来源:互联网 发布:windows php 定时任务 编辑:程序博客网 时间:2024/06/03 13:19
Q:
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.
A:
1. c++
class Solution {
public:
int hammingDistance(int x, int y) {
int hamming_distance = 0;
while(true)
{
if(x%2 != y%2)
hamming_distance += 1;
if((x/2 == 0)&&(y/2 == 0))
break;
x = x/2;
y = y/2;
}
return hamming_distance;
}
};
2. java
public class Solution { public int hammingDistance(int x, int y) { return Integer.bitCount(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
- 啥是Vue ???
- 堆优化的prim算法
- 如何快速的搭建Apache+MySQL+PHP+PERL的环境
- 剑指offer 之 字符串的全排列、全组合
- 【重装系统】Ubuntu系统重装为windows10
- leetcode-461. Hamming Distance
- 命令行
- 安卓自定义View基础06-View的onMeasure(),onDraw()方法详解以及Padding的处理
- 接口、抽象类和类之间的关系
- mysql5.7新特性解析
- ubuntu WPS 提示“系统缺失字体symbol、wingdings、wingdings 2、wingdings 3、webding”的解决方法
- JS的事件处理程序
- javascript面试考点
- js实现带缩略图的图片转换