当前位置:网站首页>LeetCode #461 汉明距离
LeetCode #461 汉明距离
2022-07-06 09:13:00 【三笠·阿卡曼】
题目
两个整数之间的 汉明距离 指的是这两个数字对应二进制位不同的位置的数目。
给你两个整数 x 和 y,计算并返回它们之间的汉明距离。
示例
最佳代码
package com.vleus.algorithm.bit_operator;
/** * @author vleus * @date 2021年08月03日 22:35 */
public class HammingDistance {
//方法一:异或:调库统计1的个数
public int hammingDistance(int x, int y) {
return Integer.bitCount(x ^ y);
}
//方法二:自定义实现统计1的个数,逐位右移
public int hammingDistance2(int x, int y) {
int xor = x ^ y; //得到异或结果
int count = 0; //保存当前1的个数
//逐位右移,直到结果为0
while (xor != 0) {
//如果最后一位为1,count++
if ((xor & 1) == 1) {
count++;
}
xor >>=1; //右移1位
}
return count;
}
public int hammingDistance3(int x, int y) {
int xor = x ^ y; //得到异或结果
int count = 0; //保存当前1的个数
//快速位移,每次寻找当前最右面的一个1,直接消去
while (xor != 0) {
xor = xor & xor - 1;
count++;
}
return count;
}
}
边栏推荐
- [C language foundation] 04 judgment and circulation
- MySQL完全卸载(Windows、Mac、Linux)
- Kubesphere - deploy the actual combat with the deployment file (3)
- February 13, 2022 - Maximum subarray and
- The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
- C language advanced pointer Full Version (array pointer, pointer array discrimination, function pointer)
- Global and Chinese market of transfer switches 2022-2028: Research Report on technology, participants, trends, market size and share
- CSDN question and answer module Title Recommendation task (I) -- Construction of basic framework
- Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
- MySQL25-索引的创建与设计原则
猜你喜欢
La table d'exportation Navicat génère un fichier PDM
MySQL25-索引的创建与设计原则
MySQL主从复制、读写分离
A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
Mysql30 transaction Basics
CSDN Q & a tag skill tree (V) -- cloud native skill tree
MySQL27-索引优化与查询优化
MySQL19-Linux下MySQL的安装与使用
CSDN-NLP:基于技能树和弱监督学习的博文难度等级分类 (一)
随机推荐
【博主推荐】asp.net WebService 后台数据API JSON(附源码)
[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
CSDN question and answer module Title Recommendation task (II) -- effect optimization
Navicat 導出錶生成PDM文件
CSDN问答模块标题推荐任务(二) —— 效果优化
How to find the number of daffodils with simple and rough methods in C language
Baidu Encyclopedia data crawling and content classification and recognition
How to change php INI file supports PDO abstraction layer
Just remember Balabala
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
虚拟机Ping通主机,主机Ping不通虚拟机
windows下同时安装mysql5.5和mysql8.0
Mysql35 master slave replication
Mysql23 storage engine
Mysql34 other database logs
La table d'exportation Navicat génère un fichier PDM
API learning of OpenGL (2001) gltexgen
MySQL33-多版本并发控制
C language string function summary
Solution: log4j:warn please initialize the log4j system properly