1. 题目链接:https://leetcode.com/problems/hamming-distance/description/

2.思路

常规做法做完看到评论区一个非常有意思的做法。用了n&=(n-1),这个地方的意思是,将最右边的1变成0。比方说:

最简单的例子:

原数字: 101011

n-1: 101010

n&(n-1):101011&101010=101010

再看另一个例子:

原数字:10100

n-1: 10011

n&(n-1):10100&10011 = 10000

最后一个极端情况:

原数字:10000

n-1:01111

n&(n-1):10000&01111=00000

3.代码

(1)评论区的解法

class Solution {
public:
int hammingDistance(int x, int y) {
int n = x^y, hd = 0;
while(n)
{
hd++;
n &= (n-1);
}
return hd;
}
};

(2)常规解法

class Solution {
public:
int hammingDistance(int x, int y) {
int n = x^y, hd = 0;
while(n)
{
hd += (n % 2);
n = n >> 1;
}
return hd;
}
};

  

Leetcode - 461. Hamming Distance n&=(n-1) (C++)的更多相关文章

  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 (汉明距离)

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

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

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

  5. 4. leetcode 461. 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 ...

  7. LeetCode 461. Hamming Distance (C++)

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

  8. [LeetCode] 461. Hamming Distance(位操作)

    传送门 Description The Hamming distance between two integers is the number of positions at which the co ...

  9. [Leetcode/Javascript] 461.Hamming Distance

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

随机推荐

  1. HTML5视频播放插件 video.js介绍

    video.js是一款很流行的html5视频播放插件.很适合在移动端播放视频(比如微信网页),功能强大,且支持降级到flash,兼容ie8.官网:http://videojs.com/    git& ...

  2. MyBatis之properties配置

    这些属性都是可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过 properties 元素的子元素来传递.例如: <properties resource=" ...

  3. chromium之scoped_ptr

    看看怎么使用 // Scopers help you manage ownership of a pointer, helping you easily manage the // a pointer ...

  4. 编程界失传秘术,SSO单点登录,什么是单点,如何实现登录?

    单点登录 多系统,单一位置登录,实现多系统同时登录的一种技术. 常出现在互联网应用和企业级平台中. 如:京东. 单点登录一般是用于互相授信的系统,实现单一位置登录,全系统有效的. 三方登录:某系统,使 ...

  5. 对于gitHub的总结随笔

    作用:用于项目的版本管理     密切相关的是       git                操作  1.本地的文件上传到github上                              ...

  6. yii学习笔记(6),数据库操作(增删改)

    数据库增删改操作通过活动记录实例来完成 插入记录 /* ----------添加记录---------- */ // 创建活动记录对象 $article = new Article(); $artic ...

  7. 『Python基础-9』元祖 (tuple)

    『Python基础-9』元祖 (tuple) 目录: 元祖的基本概念 创建元祖 将列表转化为元组 查询元组 更新元组 删除元组 1. 元祖的基本概念 元祖可以理解为,不可变的列表 元祖使用小括号括起所 ...

  8. fiddler响应报文的headers属性详解

    fiddler响应报文的headers属性详解 (1)Cache头域 1. Cache-Control 在请求报文已经说过了,用于设置缓存的属性,浏览内容不被缓存. 2. Data 生成消息的具体时间 ...

  9. Scala学习笔记(三)—— 方法和函数

    1. 方法 方法使用 def 定义: def 方法名(参数名:参数列表,…) :返回值类型 = { 方法结构体 } scala> def add(x : Int ,y : Int):Int = ...

  10. 使用idea上传项目到gitHub

    上传项目到gitHub 创建好后开始提交本地项目代码如图: 选中VCS选中图中的按钮如图所示: 然后再选中Src点中add按钮如图所示: 然后点中commit Directory后 打开终端进行项目根 ...