汉明距离
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
给出两个整数 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. 【题解】洛谷P1311 [NOIP2011TG] 选择客栈(递推)

    题目来源:洛谷P1311 思路 纯暴力明显过不了这道题 所以我们要考虑如何优化到至多只能到nlogn 但是我们发现可以更优到O(n) 我们假设我们当前寻找的是第二个人住的客栈i 那么第一个人住的客栈肯 ...

  2. mysql导出数据很快,导入很慢

    mysql导出快,导入特别慢的解决方法:在导入时添加两个参数:max_allowed_packet; net_buffer_length --max_allowed_packet   客户端/服务器之 ...

  3. chromium之MessagePump.h

    上代码,注释已经写得很详细了. 粗看一下,这是个纯虚类,用于跨平台的通用接口. MessagePump,Pump的意思是泵,,MessagePump也就是消息泵,输送消息 namespace base ...

  4. ubuntu安装flashplayer插件三步走

    1.去官网下载flash;2.解压3.复制.so文件到~/.mozilla/plugins/

  5. The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.报该错误的一种原因。

    今天发现某个action返回404. HTTP Status 404 – Not Found Type Status Report Message /xxx.action Description Th ...

  6. Pagination

    using System.Collections.Generic; namespace Oyang.Tool { public interface IPagination { int PageInde ...

  7. 爬取豆瓣电影Top250

    1 import json import requests from requests.exceptions import RequestException import re import time ...

  8. Flask之蓝图的使用

    蓝图,听起来就是一个很宏伟的东西 在Flask中的蓝图 blueprint 也是非常宏伟的 它的作用就是将 功能 与 主服务 分开怎么理解呢? 比如说,你有一个客户管理系统,最开始的时候,只有一个查看 ...

  9. nginx 同一域名下分目录配置显示php,html,资源文件

    安装上nginx后 注意后nginx.conf 中的这么几行 error_log /var/log/nginx/error.log;  日志,这个很有用 include /etc/nginx/conf ...

  10. 1. Linux内核的配置与裁减:

    一.内核的配置和编译流程: 1)编写driver及其子目录下的Kconfig文件,将驱动的配置项写入menuconfig配置界面:2)  执行make menuconfig命令,进入内核配置界面,将对 ...