Leetcode - 461. Hamming Distance n&=(n-1) (C++)
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++)的更多相关文章
- LeetCode:461. Hamming Distance
package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...
- Leetcode#461. Hamming Distance(汉明距离)
题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...
- LeetCode 461. Hamming Distance (汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [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 ...
- LeetCode 461 Hamming Distance 解题报告
题目要求 The Hamming distance between two integers is the number of positions at which the corresponding ...
- LeetCode 461. Hamming Distance (C++)
题目: The Hamming distance between two integers is the number of positions at which the corresponding ...
- [LeetCode] 461. Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...
- [Leetcode/Javascript] 461.Hamming Distance
[Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...
随机推荐
- Knowledge Point 20180305 十进制转换成二进制浮点数
如何将十进制的浮点数 转换二进制的浮点数,分为两部分: 1. 先将整数部分转换为二进制, 2. 将小数部分转换为二进制, 然后将整数部分与小数部分相加. 以 20.5 转换为例,20转换后变为1010 ...
- ConfigurationManager.AppSettings方法
一 配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是conf ...
- jquery的JSON字符串处理、单引号双引号的转换
1.jquery的JSON字符串处理 var pwdlevel_val = "{"minLength":1,"maxLength":20," ...
- php 计算两个文件的相对路径
<?php /** * 计算两个文件的相对路径 */ function relative_path($path1, $path2) { $arr1 = explode('/', dirname( ...
- Python中的封装,继承和多态
面向对象的三大特性:封装,继承和多态 封装:在类的内部定义属性和方法,通过对象或类名来访问属性和方法,隐藏功能的实现细节,也可以设置访问权限. 广义的封装:实例化一个对象,给对象空间封装一些属性:狭义 ...
- java并发(1)
hashmap效率高单线程不安全,hashTable效率低但线程安全 因为hashTable使用synchronized来保证线程安全,所以效率十分低,比如线程1使用put插入数据时,线程2既不能使用 ...
- linux进程篇 (三) 进程间的通信1 管道通信
通信方式分4大类: 管道通信:无名管道 有名管道 信号通信:发送 接收 和 处理 IPC通信:共享内存 消息队列 信号灯 socke 网络通信 用户空间 进程A <----无法通信----> ...
- Div标签使用inline-block有间距
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 20145202马超 2016-2017-2 《Java程序设计》第11周学习总结
20145202马超 2016-2017-2 <Java程序设计>第11周学习总结 教材学习内容总结 XX 教材学习中的问题和解决过程 教材学习有问题先去https://shimo.im/ ...
- QtChart 初体验
早就知道 Qt 5.7 中引入了 QtChart 模块.一直没时间试用.周末正好空闲,就简单的试了试 QtChart.QtChart 学起来还是挺简单的,基于 Qt Graphics View Fra ...