[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 ...
随机推荐
- java 多态 (知识点不完整)
1.多态的条件 1.要有继承 2.方法重写 3.父类引用指向子类对象 多态中成员访问 1. 访问成员变量: 编译看左边,运行也看左边 f.num = 10 因为这里是父类的,看是父类Father ...
- Dynamic Mapping和常见字段类型
原文:Dynamic Mapping和常见字段类型 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn. ...
- Java JSON入门
一.所需jar包 jakarta commons-lang 2.5jakarta commons-beanutils 1.8.0jakarta commons-collections 3.2.1jak ...
- 《快学scala》读书笔记(2)
第二章 控制结构和函数 1.条件表达式 (1)scala中if/else表达式有值,这个值就是跟在if或者else之后的表达式的值.如: if (x > 0) 1 else -1 这个表达式的 ...
- struts2导入多个xml引入报错<include>
struts.xml <?xml version="1.0" encoding="UTF-8"?> <!-- 指定Struts 2配置文件的D ...
- Python3安装教程
目录 1. 推荐阅读 2. 安装包下载 3. 安装步骤 1. 推荐阅读 Python基础入门一文通 | Python2 与Python3及VSCode下载和安装.PyCharm破解与安装.Python ...
- webpack 4.0 报错
ERROR in multi ./src/entery.js dist/bundle.jsModule not found: Error: Can't resolve 'dist/bundle.js' ...
- OpenVINO 安装及使用
安装 https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_linux.html 使用 文档 ...
- python常用函数 Y
yield有点像return,但他会在下一次执行的时候从上次结束点继续执行,带有 yield 的函数在 Python 中被称之为 generator(生成器),生成器无法通过索引获取数据,同时也承诺使 ...
- file 显示文件的类型
1. 命令功能 file命令是确定文件类型,也可以辨识一些文件的编码格式.通过文件的头部信息来获取文件类型.windows是通过扩展名来确定文件类型. 2. 语法格式 file [option] ...