[LeetCode] 461. Hamming Distance 汉明距离
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.
两个数字之间的汉明距离是其二进制数对应位不同的个数,两个数异或,把为1的数量累加起来就是汉明距离。
Java:
public int hammingDistance(int x, int y) {
int res = x ^ y;
int count = 0;
for (int i = 0; i < 32; i++) {
if ((res & 1) != 0)
count++;
res >>= 1;
}
return count;
}
Python:
class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
distance = 0
z = x ^ y
while z:
distance += 1
z &= z - 1
return distance
Python:
class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
return bin(x ^ y).count('1')
C++:
class Solution {
public:
int hammingDistance(int x, int y) {
int res = 0;
for (int i = 0; i < 32; ++i) {
if ((x & (1 << i)) ^ (y & (1 << i))) {
++res;
}
}
return res;
}
};
C++:
class Solution {
public:
int hammingDistance(int x, int y) {
int res = 0, exc = x ^ y;
while (exc) {
++res;
exc &= (exc - 1);
}
return res;
}
};
C++:
class Solution {
public:
int hammingDistance(int x, int y) {
if ((x ^ y) == 0) return 0;
return (x ^ y) % 2 + hammingDistance(x / 2, y / 2);
}
};
类似题目:
[LeetCode] 191. Number of 1 Bits 二进制数1的个数
All LeetCode Questions List 题目汇总
[LeetCode] 461. Hamming Distance 汉明距离的更多相关文章
- Leetcode#461. Hamming Distance(汉明距离)
题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...
- LeetCode:461. Hamming Distance
package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...
- LeetCode 461. Hamming Distance (汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] 461. Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...
- 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 ...
- 461. Hamming Distance(汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- Leetcode - 461. Hamming Distance n&=(n-1) (C++)
1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法.用了n&a ...
随机推荐
- springboot 2.2.1默认跳到登录页
最新的springboot 2.2.1版本,启动之后访问http://localhost:8080 会直接跳转到默认登录页,是由于springboot默认配置了安全策略,在启动类中忽略该配置即可 在启 ...
- python测试开发django-rest-framework-61.权限认证(permission)
前言 用户登录后,才有操作当前用户的权限,不能操作其它人的用户,这就是需要用到权限认证,要不然你登录自己的用户,去操作别人用户的相关数据,就很危险了. authentication是身份认证,判断当前 ...
- Dubbo源码分析:ProxyFactory
roxyFactory将对外开放的服务进行封装.这里使用到代理的方式.ProxyFactory接口有两个不同的实现类:JavassistProxyFactory和JdkProxyFactory.Jdk ...
- 3.Vue 实例
创建一个 Vue 实例 每个 Vue 应用都是通过用 Vue 函数创建一个新的 Vue 实例开始的: var vm = new Vue({ // 选项 }) 虽然没有完全遵循 MVVM 模型,但是 V ...
- 第三章 - SQL基础及元数据获取
SQL的介绍 SQL的定义:结构化查询语句 SQL的作用:对库和表进行操作 SQL的常用分类 DDL 数据定义语言(Data Definition Language) DCL 数据控制语言(Data ...
- Java 用Jackson进行json和object之间的转换(并解决json中存在新增多余字段的问题)
1.添加jackson库 如果是maven工程,需要在pom.xml中添加jackson的依赖: <dependency> <groupId>com.fasterxm ...
- PyCharm中Dictionary与Python package的区别
Dictionary Dictionary在pycharm中就是一个文件夹,放置资源文件,对应于在进行JavaWeb开发时用于放置css/js文件的目录,或者说在进行物体识别时,用来存储背景图像的文件 ...
- flume 测试 hive sink
测试flume,将数据送到hive表中,首先建表. create table order_flume( order_id string, user_id string, eval_set string ...
- 实现MyBatis批量查询
Service public List<DeviceBean> getDeviceList(Map<String, Object> parameter);Serv ...
- 新版本Mariadb安装后相关问题的解决
给新机器Ubuntu安装的Mariadb后无法登录,通过网上各种方法修改root用户密码,仍然无法解决,耗费几个小时! 经过看日志和查手册,发现原因如下: ubuntu确实安装没有启用root用户,所 ...