当前位置:网站首页>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;
}
}
边栏推荐
- ++Implementation of I and i++
- C language string function summary
- [reading notes] rewards efficient and privacy preserving federated deep learning
- [recommended by bloggers] asp Net WebService background data API JSON (with source code)
- Database middleware_ MYCAT summary
- 记某公司面试算法题:查找一个有序数组某个数字出现的次数
- @controller,@service,@repository,@component区别
- Copy constructor template and copy assignment operator template
- Pytoch LSTM implementation process (visual version)
- 解决:log4j:WARN Please initialize the log4j system properly.
猜你喜欢
Some problems in the development of unity3d upgraded 2020 VR
Win10: how to modify the priority of dual network cards?
CSDN question and answer module Title Recommendation task (II) -- effect optimization
[recommended by bloggers] background management system of SSM framework (with source code)
Installation and use of MySQL under MySQL 19 Linux
Mysql21 - gestion des utilisateurs et des droits
A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
Idea import / export settings file
【博主推荐】C#生成好看的二维码(附源码)
Pytoch LSTM implementation process (visual version)
随机推荐
1. Mx6u learning notes (VII): bare metal development (4) -- master frequency and clock configuration
Bytetrack: multi object tracking by associating every detection box paper reading notes ()
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
MySQL24-索引的数据结构
Mysql25 index creation and design principles
API learning of OpenGL (2005) gl_ MAX_ TEXTURE_ UNITS GL_ MAX_ TEXTURE_ IMAGE_ UNITS_ ARB
Mysql27 index optimization and query optimization
csdn-Markdown编辑器
Mysql35 master slave replication
Pytoch LSTM implementation process (visual version)
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
Copie maître - esclave MySQL, séparation lecture - écriture
Win10: how to modify the priority of dual network cards?
[paper reading notes] - cryptographic analysis of short RSA secret exponents
Global and Chinese market of operational amplifier 2022-2028: Research Report on technology, participants, trends, market size and share
MySQL33-多版本并发控制
Ansible practical Series II_ Getting started with Playbook
MySQL 20 MySQL data directory
Database middleware_ MYCAT summary