[LeetCode] 461. Hamming Distance(位操作)
Description
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.
思路
题意:给定两个数,求出其汉明距离
题解:将两个数异或,那么二进制位上相同的都变成0,不同变成1,统计一下有多少个1即可
class Solution {
public:
//6ms
int hammingDistance(int x, int y) {
int res = 0;
while (x && y){
if ((x & 1) != (y & 1)) res++;
x >>= 1;
y >>= 1;
}
while (x){
if (x & 1) res++;
x >>= 1;
}
while (y){
if (y & 1) res++;
y >>= 1;
}
return res;
}
//3ms
int hammingDistance(int x,int y){
x ^= y;
y = 0;
while (x){
y += (x & 1);
x >>= 1;
}
return y;
}
};
[LeetCode] 461. Hamming Distance(位操作)的更多相关文章
- 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 n&=(n-1) (C++)
1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法.用了n&a ...
- [Leetcode/Javascript] 461.Hamming Distance
[Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...
随机推荐
- docker pull使用 代理
[root@fdfs- ~]# cat /usr/lib/systemd/system/docker.service [Unit] Description=Docker Application Con ...
- CSS利用border绘制图性
绘制梯形 width:10px; border-top:10px solid red; border-right:10px solid transparent; border-left:10px so ...
- JavaScript深入之从原型到原型链(转载)
构造函数创建对象 我们先使用构造函数创建一个对象: function Person() { } var person = new Person(); person.name = 'Kevin'; co ...
- 阿里开源框架-JarsLink-【JAVA的模块化开发框架】
JarsLink (原名Titan) 是一个基于JAVA的模块化开发框架,它提供在运行时动态加载模块(一个JAR包).卸载模块和模块间调用的API. 需求背景 应用拆分的多或少都有问题.多则维护成本高 ...
- 【记录】form-data与x-www-form-urlencoded的区别
1)application/x-www-form-urlencoded 这应该是最常见的 POST 提交数据的方式了.浏览器的原生 <form> 表单,如果不设置 enctype 属性,那 ...
- 阿里P8技术栈
- hadoop HA + HBase HA搭建:
hadoop HA搭建参考:https://www.cnblogs.com/NGames/p/11083640.html (本节:用不到YARN 所以可以不用考虑部署YARN部分) Hadoop 使用 ...
- python基础--3 列表
#list类#li是list类的一个对象li=[11,22,33,22,44] #参数#在原来值最后进行整个作为元素追加 # li.append((11,22,33))#对列表本身进行操作,appen ...
- tomcat启动报错:Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
1.错误信息: 严重: Error configuring application listener of class org.springframework.web.context.ContextL ...
- Linux ssh内置sftp配置说明
centos7 环境下已验证 首先建立两个用户,用于sftp访问使用. eg: useradd -d /opt/sftp -s /bin/nologin sftp 说明 -s /bin/nologi ...