当前位置:网站首页>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;
}
}
边栏推荐
- 虚拟机Ping通主机,主机Ping不通虚拟机
- Invalid default value for 'create appears when importing SQL_ Time 'error reporting solution
- CSDN question and answer tag skill tree (II) -- effect optimization
- Win10: how to modify the priority of dual network cards?
- Postman Interface Association
- MySQL22-逻辑架构
- Mysql24 index data structure
- IDEA 导入导出 settings 设置文件
- Moteur de stockage mysql23
- [ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
猜你喜欢

MySQL28-数据库的设计规范

MySQL 20 MySQL data directory

Emotional classification of 1.6 million comments on LSTM based on pytoch

Other new features of mysql18-mysql8

CSDN-NLP:基于技能树和弱监督学习的博文难度等级分类 (一)

Mysql23 storage engine

Postman environment variable settings

Why is MySQL still slow to query when indexing is used?

MySQL transaction log

Just remember Balabala
随机推荐
【博主推荐】SSM框架的后台管理系统(附源码)
Navicat 导出表生成PDM文件
Are you monitored by the company for sending resumes and logging in to job search websites? Deeply convinced that the product of "behavior awareness system ba" has not been retrieved on the official w
A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
Have you mastered the correct posture of golden three silver four job hopping?
JDBC原理
MySQL transaction log
[C language foundation] 04 judgment and circulation
MySQL23-存储引擎
MySQL主從複制、讀寫分離
Global and Chinese markets for aprotic solvents 2022-2028: Research Report on technology, participants, trends, market size and share
MySQL30-事务基础知识
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
Other new features of mysql18-mysql8
MySQL27-索引优化与查询优化
MySQL18-MySQL8其它新特性
Install mysql5.5 and mysql8.0 under windows at the same time
[leectode 2022.2.13] maximum number of "balloons"
MySQL23-存儲引擎
Mysql22 logical architecture