两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。

给出两个整数 x 和 y,计算它们之间的汉明距离。

注意:

0 ≤ x, y < 231.

示例:

输入: x = 1, y = 4

输出: 2

解释: 1 (0 0 0 1)

4 (0 1 0 0)

↑ ↑ 上面的箭头指出了对应二进制位不同的位置。

class Solution {
public:
int hammingDistance(int x, int y) {
int cnt = 0;
int temp = x ^ y;
while(temp)
{
if(temp & 1 == 1)
cnt++;
temp = temp >> 1;
}
return cnt;
}
};

Leetcode461Hamming Distance汉明距离的更多相关文章

  1. [LeetCode] Hamming Distance 汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  2. [LeetCode] 461. Hamming Distance 汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  3. 477 Total Hamming Distance 汉明距离总和

    两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量.计算一个数组中,任意两个数之间汉明距离的总和.示例:输入: 4, 14, 2输出: 6解释: 在二进制表示中,4表示为0100,14表 ...

  4. 461. Hamming Distance(汉明距离)

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  5. NLP&数据挖掘基础知识

    Basis(基础): SSE(Sum of Squared Error, 平方误差和) SAE(Sum of Absolute Error, 绝对误差和) SRE(Sum of Relative Er ...

  6. 常用的机器学习&数据挖掘知识点【转】

    转自: [基础]常用的机器学习&数据挖掘知识点 Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Le ...

  7. 【基础】常用的机器学习&数据挖掘知识点

    Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Least Square Methods 最小二乘法),ML ...

  8. 海量数据挖掘MMDS week2: LSH的距离度量方法

    http://blog.csdn.net/pipisorry/article/details/48882167 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...

  9. 常用的机器学习&数据挖掘知识(点)总结

    Basis(基础): MSE(Mean Square Error 均方误差), LMS(LeastMean Square 最小均方), LSM(Least Square Methods 最小二乘法), ...

随机推荐

  1. 一个mdl相关的问题

    感觉挺有意义的,细节问题 http://www.kernelmode.info/forum/viewtopic.php?f=14&t=4318这个人代码到底犯了什么错误 I want to p ...

  2. 线性回归代码实现(matlab)

    1 代价函数实现(cost function) function J = computeCost(X, y, theta) %COMPUTECOST Compute cost for linear r ...

  3. spark jdk8 单词统计示例

    在github上有spark-java8 实例地址: https://github.com/ypriverol/spark-java8 https://github.com/ihr/java8-spa ...

  4. nginx源码分析-源码结构

    本文主要简单介绍nginx源码目录结构.程序编译流程.如何构建学习nginx的环境等.本文以及后续nginx源码分析文章是基于nginx当前(2009-02-27)的稳定版本0.6.35进行的分析,该 ...

  5. opencv-图像遍历

    #include "stdafx.h" #include<opencv2/opencv.hpp> #include<iostream> #include&l ...

  6. js获取json的健与值

    let myObj = { name: '张三', age: 18,sex:'女' } let tempArr = Object.keys(myObj) console.log(tempArr) fo ...

  7. C#一般处理程序设置和读取session(session报错“未将对象引用设置到对象的实例”解决)

    登陆模块时,用到了session和cookie.在一般处理程序中处理session,一直报错.最后找到问题原因是需要调用 irequiressessionstate接口. 在ashx文件中,设置ses ...

  8. 修改cmd命令默认路径

    未修改之前: 修改方法: 1.win+r打开运行对话框,输入 regedit 打开注册表编辑器 2.在注册表中找到:HKEY_CURRENT_USER\Software\Microsoft\Comma ...

  9. MyEclipse使用总结——将原有的MyEclipse中的项目转成maven项目[转]

    前面一篇文章中我们了解了 在myeclipse中新建Maven框架的web项目 那么如果我们原来有一些项目现在想转成maven项目应该怎么做呢 我收集到了三种思路: 一.新建一个maven项目,把原项 ...

  10. maven 工程搭建

    使用Maven建立一个Quite start 项目 2.命名卫 bhz-parent 3.groupid 为:  bhz 4.artifactId 为: bhz-parent package:为空不填 ...