// 快速法求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. vscode使用vue中的v-for提示错误

    "vetur.validation.template": false 在设置里面把vetur.validation.template改为false 文件→首选项→设置 搜索vetu ...

  2. 手机端的META差异

    手机端的META你了解多少? 我们先来简单了解下meta标签:meta指元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. 标签位于文档的头部, ...

  3. 基于R语言的数据分析和挖掘方法总结——均值检验

    2.1 单组样本均值t检验(One-sample t-test) 2.1.1 方法简介 t检验,又称学生t(student t)检验,是由英国统计学家戈斯特(William Sealy Gosset, ...

  4. level-8

    CSS选择器常见的有几种? !important选择器——它是所有选择器里面权重最高的. *选择器——它是所有选择器里面权重最低的.可以为页面所有的标签添加统一样式,多用于页面整体样式调整,但它会增加 ...

  5. git 使用教程 --基础二

    一:分支学习: branch称为分支,默认仅有一个名为master的分支.一般开发新功能流程为:开发新功能时会在分支dev上进行,开发完毕后再合并到master分支. 分支的作用: 创建分支:(创建分 ...

  6. PHP用*隐藏中文问题

    中文截取 function substr_cut($user_name){ $strlen= mb_strlen($user_name, 'utf-8'); $firstStr= mb_substr( ...

  7. 轻量级的Java 开发框架 Spring

    Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development a ...

  8. python爬取糗事百科段子

    初步爬取糗事百科第一页段子(发布人,发布内容,好笑数和评论数) #-*-coding:utf--*- import urllib import urllib2 import re page = url ...

  9. ZooKeeper-znode概念与使用

    可以将ZK看作一个具有高可用性特征的文件系统.这个文件系统中没有文件和目录,而是统一使用节点(znode)的概念,称为znode.znode既可以作为保存数据的容器(如同文件),也可以作为保存其他zn ...

  10. ZSTU 4241 圣杯战争(线段树+经典)

    题意:CS召唤了n个实验怪兽,第i号怪兽在i这个位置出.并把KI召唤出的第i位从者安排在pos(i)处,总共有m位从者. 第i只怪兽有战斗力atk(i), 而i号从者的体力为AP(i).如果从者想要移 ...