当前位置:网站首页>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;
}
}
边栏推荐
- How to find the number of daffodils with simple and rough methods in C language
- Asp access Shaoxing tourism graduation design website
- Water and rain condition monitoring reservoir water and rain condition online monitoring
- [recommended by bloggers] background management system of SSM framework (with source code)
- February 13, 2022-2-climbing stairs
- [untitled]
- [BMZCTF-pwn] 12-csaw-ctf-2016-quals hungman
- MySQL21-用户与权限管理
- C语言标准的发展
- Solve the problem that XML, YML and properties file configurations cannot be scanned
猜你喜欢

Postman uses scripts to modify the values of environment variables

Mysql28 database design specification

CSDN markdown editor

MySQL33-多版本并发控制

MySQL24-索引的数据结构

CSDN Q & a tag skill tree (V) -- cloud native skill tree

35 is not a stumbling block in the career of programmers

MySQL19-Linux下MySQL的安装与使用

Postman environment variable settings

Swagger、Yapi接口管理服务_SE
随机推荐
MySQL19-Linux下MySQL的安装与使用
frp内网穿透那些事
MySQL completely uninstalled (windows, MAC, Linux)
[paper reading notes] - cryptographic analysis of short RSA secret exponents
Case identification based on pytoch pulmonary infection (using RESNET network structure)
Mysql22 logical architecture
MySQL master-slave replication, read-write separation
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
Baidu Encyclopedia data crawling and content classification and recognition
Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
API learning of OpenGL (2005) gl_ MAX_ TEXTURE_ UNITS GL_ MAX_ TEXTURE_ IMAGE_ UNITS_ ARB
MySQL flush operation
虚拟机Ping通主机,主机Ping不通虚拟机
Install mysql5.5 and mysql8.0 under windows at the same time
[untitled]
Redis的基础使用
Detailed reading of stereo r-cnn paper -- Experiment: detailed explanation and result analysis
Why is MySQL still slow to query when indexing is used?
MySQL22-逻辑架构
Mysql24 index data structure