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. JAVA数据类型能串门不?

    JAVA这几种数据类型,能否串门?入了人家门,就得按人家规矩来,入乡随俗哦,难免发生有自觉的 还有不情愿被动的. 自动类型转换 自动类型转换:容量小的数据类型可以自动转换为容量大的数据类型.在图中,黑 ...

  2. 一个 Safari 的 new Date() bug

    开发「bufpay.com 个人即时到账收款平台」后台套餐修改功能的时候碰到一个 new Date() bug. 既在 Safari 里面不支持 var t = new Date('2018-06-1 ...

  3. 811. Subdomain Visit Count (5月23日)

    解答 class Solution { public: vector<string> subdomainVisits(vector<string>& cpdomains ...

  4. Linux Centos6.5 升级默认Python2.6.6到Python2.7.13

    以下例子基于python 2.7.9,其他版本同理.大致的命令都是差不多的,安装完成之后,输入Python --vertion ,看到系统默认的版本已经替换为2.7版本了 1.下载python wge ...

  5. 前台页面上传data image图片,java后台接收图片保存

    最近在项目中有这么一个需求,就是上传一个视频文件,然后要获取视频文件的第一帧图片,这个可以通过canvas获取得到,得到的是一个dataURL,之后还要将这个图片上传到云,这个时候如何操作就不清楚了, ...

  6. Java中connection的常用方法及其描述是什么

    1. close(), 关闭该数据库连接2. commit(), 提交所有更改内容并释放该Connection对象锁定的资源3. createStatement(), 基于本Connection对象, ...

  7. Python-爬虫小计

    # -*-coding:utf8-*-import requestsfrom bs4 import BeautifulSoupimport timeimport osimport urllibimpo ...

  8. Java三种代理模式

    本文转自:https://mp.weixin.qq.com/s/nBmbNP2mR7ei-lDsuOxjWg 代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象 ...

  9. 大数据学习--day06(Eclipse、数组)

    Eclipse.数组 Eclipse 的基本设置   调节控制条字体大小. Window -> Preferences -> General -> Appearance -> ...

  10. C语言中malloc函数的理解

    在C语言中malloc函数主要是用在堆内存的申请上,使用malloc函数时,函数会返回一个void *类型的值,这个值就是你申请的堆内存的首地址:为什么返回的地址是一个void *类型的地址呢?首先我 ...