当前位置:网站首页>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;
}
}
边栏推荐
- 【博主推荐】asp.net WebService 后台数据API JSON(附源码)
- MySQL18-MySQL8其它新特性
- [recommended by bloggers] C WinForm regularly sends email (with source code)
- 记一次某公司面试题:合并有序数组
- C language string function summary
- Install mysql5.5 and mysql8.0 under windows at the same time
- Asp access Shaoxing tourism graduation design website
- MySQL22-逻辑架构
- Mysql21 - gestion des utilisateurs et des droits
- MySQL完全卸载(Windows、Mac、Linux)
猜你喜欢
MySQL23-存儲引擎
1. Mx6u learning notes (VII): bare metal development (4) -- master frequency and clock configuration
Install mysql5.5 and mysql8.0 under windows at the same time
CSDN问答模块标题推荐任务(二) —— 效果优化
Navicat 导出表生成PDM文件
windows无法启动MYSQL服务(位于本地计算机)错误1067进程意外终止
CSDN-NLP:基于技能树和弱监督学习的博文难度等级分类 (一)
【博主推荐】C#生成好看的二维码(附源码)
Mysql27 - Optimisation des index et des requêtes
Win10: how to modify the priority of dual network cards?
随机推荐
记某公司面试算法题:查找一个有序数组某个数字出现的次数
Are you monitored by the company for sending resumes and logging in to job search websites? Deeply convinced that the product of "behavior awareness system ba" has not been retrieved on the official w
C language string function summary
Mysql33 multi version concurrency control
[Li Kou 387] the first unique character in the string
Timestamp with implicit default value is deprecated error in MySQL 5.6
CSDN问答标签技能树(一) —— 基本框架的构建
[recommended by bloggers] C # generate a good-looking QR code (with source code)
Postman environment variable settings
Mysql25 index creation and design principles
MySQL28-数据库的设计规范
SSM整合笔记通俗易懂版
JDBC原理
MySQL31-MySQL事务日志
CSDN blog summary (I) -- a simple first edition implementation
Bytetrack: multi object tracking by associating every detection box paper reading notes ()
CSDN博文摘要(一) —— 一个简单的初版实现
MySQL24-索引的数据结构
[BMZCTF-pwn] 11-pwn111111
Emotional classification of 1.6 million comments on LSTM based on pytoch