当前位置:网站首页>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;
}
}
边栏推荐
- MySQL24-索引的数据结构
- MySQL 29 other database tuning strategies
- MySQL27-索引优化与查询优化
- Mysql 其他主机无法连接本地数据库
- 虚拟机Ping通主机,主机Ping不通虚拟机
- Solution: log4j:warn please initialize the log4j system properly
- ++Implementation of I and i++
- CSDN question and answer tag skill tree (I) -- Construction of basic framework
- MySQL18-MySQL8其它新特性
- MySQL23-存储引擎
猜你喜欢
CSDN-NLP:基于技能树和弱监督学习的博文难度等级分类 (一)
MySQL31-MySQL事务日志
Mysql26 use of performance analysis tools
[Li Kou 387] the first unique character in the string
35 is not a stumbling block in the career of programmers
Copie maître - esclave MySQL, séparation lecture - écriture
Mysql30 transaction Basics
A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
Idea import / export settings file
MySQL19-Linux下MySQL的安装与使用
随机推荐
Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
CSDN question and answer tag skill tree (II) -- effect optimization
Adaptive Bezier curve network for real-time end-to-end text recognition
MySQL 20 MySQL data directory
Postman uses scripts to modify the values of environment variables
记一次某公司面试题:合并有序数组
Install MySQL for Ubuntu 20.04
Ansible实战系列二 _ Playbook入门
Mysql30 transaction Basics
CSDN问答模块标题推荐任务(二) —— 效果优化
Kubesphere - deploy the actual combat with the deployment file (3)
SSM integrated notes easy to understand version
Solution: log4j:warn please initialize the log4j system properly
February 13, 2022-3-middle order traversal of binary tree
Detailed reading of stereo r-cnn paper -- Experiment: detailed explanation and result analysis
MySQL33-多版本并发控制
Navicat 导出表生成PDM文件
1. Mx6u learning notes (VII): bare metal development (4) -- master frequency and clock configuration
Mysql22 logical architecture
Global and Chinese markets of static transfer switches (STS) 2022-2028: Research Report on technology, participants, trends, market size and share