// 快速法求1的个数
int BitCount2(unsigned int n)
{
unsigned int c = ;
for (c =; n; ++c)
{
n &= (n -) ; // 清除最低位的1
}
return c ;
}

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 = 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.
//比较两个数二进制位的不同,输出不同的位数, 直接异或然后统计1的个数
 class Solution {
public:
int hammingDistance(int x, int y) {
int z=x^y;
int ans=;
while(z){
if(z&)
ans++;
z>>=;
}
return ans;
}
};

461. Hamming Distance Add to List的更多相关文章

  1. LeetCode:461. Hamming Distance

    package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...

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

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

  3. 【leetcode】461. Hamming Distance

    problem 461. Hamming Distance solution1: 根据题意,所求汉明距离指的是两个数字的二进制对应位不同的个数.对应位异或操作为1的累积和. class Solutio ...

  4. [Leetcode/Javascript] 461.Hamming Distance

    [Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...

  5. 461. Hamming Distance and 477. Total Hamming Distance in Python

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

  6. 4. leetcode 461. Hamming Distance

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

  7. 461. Hamming Distance(leetcode)

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

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

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

  9. LeetCode 461 Hamming Distance 解题报告

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

随机推荐

  1. mysql分组查询报错

    执行sql group by查询时报错 SELECT id from userz GROUP BY username Expression #1 of SELECT list is not in GR ...

  2. SpringMVC:学习笔记(7)——验证器(JSR303)

    JSR 303(Bean Validation ) 说明: 在任何时候,当你要处理一个应用程序的业务逻辑,数据校验是你必须要考虑和面对的事情.应用程序必须通过某种手段来确保输入进来的数据从语义上来讲是 ...

  3. JavaWeb:实现文件上传与下载

    JavaWeb:实现文件上传与下载 文件上传前端处理 本模块使用到的前端Ajax库为Axio,其地址为GitHub官网. 关于文件上传 上传文件就是把客户端的文件发送给服务器端. 在常见情况(不包含文 ...

  4. 爬虫学习笔记(2)--创建scrapy项目&&css选择器

    一.手动创建scrapy项目---------------- 安装scrapy: pip install -i https://pypi.douban.com/simple/  scrapy    1 ...

  5. $Android AlarmManager的用法详解

    在Android的Alarm机制中,使用AlarmManager可以实现类似闹钟这样的定时任务.在毕业设计项目中要实现定时任务的功能,所以在这里先进行一下梳理. (一)AlarmManager与Bro ...

  6. Django用户注册、邮箱验证实践

    算法流程如下:1)处理用户注册数据,存入数据库,is_activity字段设置为False,用户未认证之前不允许登陆2)产生token,生成验证连接URL3)发送验证邮件4)用户通过认证邮箱点击验证连 ...

  7. skynet中动态库的处理

    skynet中的.so动态库由service-src中的c文件编译完后生成,其中最重要的是snlua.c. 源码地址:https://github.com/cloudwu/skynet/service ...

  8. 关于sublime text 3 pylinter的错误提示

    刚开始用windows下sublime text 3写python,搭建完以后,按ctrl+b可以build,然后保存时候一直提示. Fatal pylint error: x:/python: ca ...

  9. 基于组的策略(GBP)开启新型网络设计时代

    在传统物理网络环境下,划分VLAN,分配网段,设置路由是个网工应该熟悉的内容.在SDN环境下,比如neutron虚拟网络,我们用API创建网络,子网,虚拟路由器,负载均衡和防火墙,这些还是太网络化了. ...

  10. setintervalue传参数的三种方法

    http://www.cnblogs.com/wkylin/archive/2012/09/07/2674911.html http://www.bhcode.net/article/20110822 ...