当前位置:网站首页>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;
}
}
边栏推荐
- Opencv uses freetype to display Chinese
- MySQL21-用戶與權限管理
- Install mysql5.5 and mysql8.0 under windows at the same time
- 【博主推荐】C#生成好看的二维码(附源码)
- Why is MySQL still slow to query when indexing is used?
- Mysql26 use of performance analysis tools
- MySQL27-索引優化與查詢優化
- NPM an error NPM err code enoent NPM err syscall open
- MySQL24-索引的数据结构
- Mysql33 multi version concurrency control
猜你喜欢
[recommended by bloggers] C # generate a good-looking QR code (with source code)
35 is not a stumbling block in the career of programmers
MySQL主從複制、讀寫分離
MySQL18-MySQL8其它新特性
Mysql21 - gestion des utilisateurs et des droits
Mysql25 index creation and design principles
CSDN question and answer module Title Recommendation task (I) -- Construction of basic framework
Postman environment variable settings
MySQL20-MySQL的数据目录
MySQL25-索引的创建与设计原则
随机推荐
Other new features of mysql18-mysql8
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
Detailed reading of stereo r-cnn paper -- Experiment: detailed explanation and result analysis
Global and Chinese market of wafer processing robots 2022-2028: Research Report on technology, participants, trends, market size and share
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
【博主推荐】asp.net WebService 后台数据API JSON(附源码)
SSM整合笔记通俗易懂版
Mysql28 database design specification
Advantages and disadvantages of evaluation methods
MySQL flush operation
Emotional classification of 1.6 million comments on LSTM based on pytoch
MySQL20-MySQL的数据目录
Copie maître - esclave MySQL, séparation lecture - écriture
MySQL23-存储引擎
Navicat 导出表生成PDM文件
MySQL other hosts cannot connect to the local database
Global and Chinese market of thermal mixers 2022-2028: Research Report on technology, participants, trends, market size and share
CSDN question and answer tag skill tree (I) -- Construction of basic framework
MySQL22-逻辑架构
MySQL 20 MySQL data directory