当前位置:网站首页>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] deeply analyze the underlying principle of data storage
- SSM整合笔记通俗易懂版
- CSDN question and answer module Title Recommendation task (I) -- Construction of basic framework
- Global and Chinese market for intravenous catheter sets and accessories 2022-2028: Research Report on technology, participants, trends, market size and share
- 导入 SQL 时出现 Invalid default value for ‘create_time‘ 报错解决方法
- Global and Chinese market of operational amplifier 2022-2028: Research Report on technology, participants, trends, market size and share
- MySQL master-slave replication, read-write separation
- Global and Chinese market of thermal mixers 2022-2028: Research Report on technology, participants, trends, market size and share
- 【博主推荐】C#生成好看的二维码(附源码)
- IDEA 导入导出 settings 设置文件
猜你喜欢
C language advanced pointer Full Version (array pointer, pointer array discrimination, function pointer)
windows下同时安装mysql5.5和mysql8.0
MySQL29-数据库其它调优策略
csdn-Markdown编辑器
Unicode decodeerror: 'UTF-8' codec can't decode byte 0xd0 in position 0 successfully resolved
解决:log4j:WARN Please initialize the log4j system properly.
Install mysql5.5 and mysql8.0 under windows at the same time
CSDN博文摘要(一) —— 一个简单的初版实现
How to change php INI file supports PDO abstraction layer
MySQL24-索引的数据结构
随机推荐
CSDN问答标签技能树(五) —— 云原生技能树
Have you mastered the correct posture of golden three silver four job hopping?
MySQL other hosts cannot connect to the local database
Generate PDM file from Navicat export table
MySQL21-用戶與權限管理
[recommended by bloggers] background management system of SSM framework (with source code)
Pytoch LSTM implementation process (visual version)
CSDN blog summary (I) -- a simple first edition implementation
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
Mysql27 - Optimisation des index et des requêtes
MySQL30-事务基础知识
Ansible实战系列三 _ task常用命令
@Controller, @service, @repository, @component differences
Mysql27 index optimization and query optimization
First blog
Ansible practical Series III_ Task common commands
Postman environment variable settings
Opencv uses freetype to display Chinese
Mysql25 index creation and design principles
【博主推荐】SSM框架的后台管理系统(附源码)