当前位置:网站首页>Leetcode 461 Hamming distance
Leetcode 461 Hamming distance
2022-07-06 11:00:00 【Sanli akaman】
subject
Between two integers Hamming distance It refers to the number of different positions of the two numbers corresponding to the binary bits .
Here are two integers x and y, Calculate and return the Hamming distance between them .
Example

Best code
package com.vleus.algorithm.bit_operator;
/** * @author vleus * @date 2021 year 08 month 03 Japan 22:35 */
public class HammingDistance {
// Method 1 : Exclusive or : Inventory adjustment statistics 1 The number of
public int hammingDistance(int x, int y) {
return Integer.bitCount(x ^ y);
}
// Method 2 : Custom implementation statistics 1 The number of , Move right bit by bit
public int hammingDistance2(int x, int y) {
int xor = x ^ y; // Get XOR result
int count = 0; // Save the current 1 The number of
// Move right bit by bit , Until the result is 0
while (xor != 0) {
// If the last one is 1,count++
if ((xor & 1) == 1) {
count++;
}
xor >>=1; // Move right 1 position
}
return count;
}
public int hammingDistance3(int x, int y) {
int xor = x ^ y; // Get XOR result
int count = 0; // Save the current 1 The number of
// Fast displacement , Look for the current rightmost one every time 1, Eliminate directly
while (xor != 0) {
xor = xor & xor - 1;
count++;
}
return count;
}
}
边栏推荐
- Have you mastered the correct posture of golden three silver four job hopping?
- Mysql21 - gestion des utilisateurs et des droits
- 虚拟机Ping通主机,主机Ping不通虚拟机
- Global and Chinese market of thermal mixers 2022-2028: Research Report on technology, participants, trends, market size and share
- Mysql27 index optimization and query optimization
- [recommended by bloggers] C # generate a good-looking QR code (with source code)
- MySQL21-用戶與權限管理
- Redis的基础使用
- Baidu Encyclopedia data crawling and content classification and recognition
- CSDN问答模块标题推荐任务(一) —— 基本框架的搭建
猜你喜欢

35 is not a stumbling block in the career of programmers

MySQL28-数据库的设计规范

Install mysql5.5 and mysql8.0 under windows at the same time

API learning of OpenGL (2003) gl_ TEXTURE_ WRAP_ S GL_ TEXTURE_ WRAP_ T

Mysql24 index data structure

MySQL主从复制、读写分离

Water and rain condition monitoring reservoir water and rain condition online monitoring

Swagger, Yapi interface management service_ SE

【博主推荐】asp.net WebService 后台数据API JSON(附源码)
![[recommended by bloggers] C # generate a good-looking QR code (with source code)](/img/5a/1dbafe5a28f016b815964b9b37c9f1.jpg)
[recommended by bloggers] C # generate a good-looking QR code (with source code)
随机推荐
Navicat 導出錶生成PDM文件
MySQL21-用戶與權限管理
[recommended by bloggers] asp Net WebService background data API JSON (with source code)
Install mysql5.5 and mysql8.0 under windows at the same time
【博主推荐】C#生成好看的二维码(附源码)
[leectode 2022.2.13] maximum number of "balloons"
Swagger、Yapi接口管理服务_SE
MySQL23-存儲引擎
Global and Chinese market of transfer switches 2022-2028: Research Report on technology, participants, trends, market size and share
[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
How to find the number of daffodils with simple and rough methods in C language
API learning of OpenGL (2002) smooth flat of glsl
导入 SQL 时出现 Invalid default value for ‘create_time‘ 报错解决方法
MySQL27-索引优化与查询优化
Moteur de stockage mysql23
Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
CSDN问答模块标题推荐任务(二) —— 效果优化
CSDN blog summary (I) -- a simple first edition implementation
【博主推荐】C# Winform定时发送邮箱(附源码)
MySQL33-多版本并发控制