当前位置:网站首页>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;
}
}
边栏推荐
- Installation and use of MySQL under MySQL 19 Linux
- 【博主推荐】asp.net WebService 后台数据API JSON(附源码)
- Valentine's Day is coming, are you still worried about eating dog food? Teach you to make a confession wall hand in hand. Express your love to the person you want
- FRP intranet penetration
- CSDN question and answer tag skill tree (I) -- Construction of basic framework
- Mysql25 index creation and design principles
- Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution
- [reading notes] rewards efficient and privacy preserving federated deep learning
- Asp access Shaoxing tourism graduation design website
- API learning of OpenGL (2004) gl_ TEXTURE_ MIN_ FILTER GL_ TEXTURE_ MAG_ FILTER
猜你喜欢

CSDN blog summary (I) -- a simple first edition implementation

Win10: how to modify the priority of dual network cards?

Case identification based on pytoch pulmonary infection (using RESNET network structure)
![[reading notes] rewards efficient and privacy preserving federated deep learning](/img/c3/5e88277b5024885d5ceeaa0de14b27.jpg)
[reading notes] rewards efficient and privacy preserving federated deep learning

A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon

Mysql22 logical architecture

Copie maître - esclave MySQL, séparation lecture - écriture

Mysql34 other database logs

【博主推荐】asp.net WebService 后台数据API JSON(附源码)

Navicat 导出表生成PDM文件
随机推荐
Texttext data enhancement method data argument
Have you mastered the correct posture of golden three silver four job hopping?
Pytoch LSTM implementation process (visual version)
Ansible实战系列二 _ Playbook入门
How to find the number of daffodils with simple and rough methods in C language
Copie maître - esclave MySQL, séparation lecture - écriture
Pytorch RNN actual combat case_ MNIST handwriting font recognition
Breadth first search rotten orange
Copy constructor template and copy assignment operator template
1. Mx6u learning notes (VII): bare metal development (4) -- master frequency and clock configuration
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
【博主推荐】C# Winform定时发送邮箱(附源码)
API learning of OpenGL (2004) gl_ TEXTURE_ MIN_ FILTER GL_ TEXTURE_ MAG_ FILTER
Mysql34 other database logs
CSDN博文摘要(一) —— 一个简单的初版实现
++Implementation of I and i++
MySQL27-索引優化與查詢優化
Redis的基础使用
API learning of OpenGL (2003) gl_ TEXTURE_ WRAP_ S GL_ TEXTURE_ WRAP_ T
[leectode 2022.2.13] maximum number of "balloons"