汉明距离
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
给出两个整数 x 和 y,计算它们之间的汉明距离。
注意:
0 ≤ x, y < 231.
示例:
输入: x = 1, y = 4
 
输出: 2
 
解释:
1 (0 0 0 1)
4 (0 1 0 0)
        ↑   ↑
 
上面的箭头指出了对应二进制位不同的位置。
 
 
^
如果存在于其中一个操作数中但不同时存在于两个操作数中,二进制异或运算符复制一位到结果中。

思路:利用了异或的性质^   两个数字的二进制字符,比如   01001^10010=11011

异或^得到的结果中含有多少个1,就表示他的汉明距离。和上一道题:位1的个数 类似https://www.cnblogs.com/patatoforsyj/p/9475383.html

代码如下:

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

leetcode-汉明距离的更多相关文章

  1. [LeetCode] Total Hamming Distance 全部汉明距离

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

  2. [LeetCode] Hamming Distance 汉明距离

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

  3. LeetCode 461 汉明距离/LintCode 365 统计二进制中1的个数

    LeetCode 461. 汉明距离 or LintCode 365. 二进制中有多少个1 题目一:LeetCode 461. 汉明距离 LeetCode 461.明距离(Hamming Distan ...

  4. 【LeetCode】汉明距离(Hamming Distance)

    这道题是LeetCode里的第461道题. 题目描述: 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x,  ...

  5. [LeetCode] 477. Total Hamming Distance 全部汉明距离

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

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

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

  7. LeetCode 461. Hamming Distance (汉明距离)

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

  8. leetCode:461 汉明距离

    汉明距离 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 思路: 当看到"对应二进制位不同的位置的数目"这 ...

  9. 从0开始的LeetCode生活—461-Hamming Distance(汉明距离)

    题目: The Hamming distance between two integers is the number of positions at which the corresponding ...

  10. Leetcode#461. Hamming Distance(汉明距离)

    题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...

随机推荐

  1. mime中间件

    mime中间件Demo,里面用到的有 1.path模块 //引入模块 var path=require('path'); 2.extname方法 //获取文件的扩展名 var extname=path ...

  2. java eclipse 访问 Oracle数据库的代码

    package com.hanqi.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.R ...

  3. Node.js_06 express

    一.初探 1 express是什么 Express.js 框架是目前最流行的node.js后端框架之一, 相当于jQuery和js之间的关系; Express 不对 Node.js 已有的特性进行二次 ...

  4. Office365学习笔记—Xslt自定义列表视图

    1,在Office365中需要添加自定义的视图!用Spd添加视图,这儿我添加一个testView! (1)打开testView.aspx将</ZoneTemplate>节点中的内容全部删除 ...

  5. Asset Catalogs

    原文见这里. Asset Catalogs用于简化管理程序内用到的图片.每个asset catalog可以包含image set, App Icon, Launch Image和OS X Icon(如 ...

  6. Java设计模式六大原则-2

    Java设计模式六大原则-2 做Java程序开发的每天都在使用JDK,Spring,SpringMvc,Mybatis,Netty,MINA等框架,但很少有人懂得背后的原理.即使打开跟下原码也是一头雾 ...

  7. JavaScript基础部分经典案例

    再复杂的程序都是由一个个简单的部分组成. 001案例 - 交换两个变量的值 方法01 - 使用临时变量 var n1 = 5; var n2 = 6; // 创建一个临时中介变量 tmp var tm ...

  8. 单文件版本的netframework的net core 2.1

    如果你还在用net4.5,如果你还在用netframework,又想使用netcore2.1的库或者功能,又觉得nuget动不动就好大,可以试试下面的这个. https://pan.baidu.com ...

  9. linux系统可执行文件添加环境变量使其跨终端和目录执行

    在命令行终端输入:echo $PATH 回车可打印出PATH变量对应的路径 现有一可执行文件qtFirstC,文件所在目录为:/home/lolors/qtFirstC 此时test只能在此目录下运行 ...

  10. C# 后台Http访问 raw from 键值对

    using RestSharp;using System;using System.Collections;using System.Collections.Generic;using System. ...