当前位置:网站首页>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;
}
}
边栏推荐
- MNIST implementation using pytoch in jupyter notebook
- Invalid default value for 'create appears when importing SQL_ Time 'error reporting solution
- CSDN问答模块标题推荐任务(二) —— 效果优化
- C language advanced pointer Full Version (array pointer, pointer array discrimination, function pointer)
- Navicat 導出錶生成PDM文件
- NPM an error NPM err code enoent NPM err syscall open
- [leectode 2022.2.13] maximum number of "balloons"
- Mysql33 multi version concurrency control
- 【博主推荐】C#MVC列表实现增删改查导入导出曲线功能(附源码)
- Mysql25 index creation and design principles
猜你喜欢

MySQL28-数据库的设计规范

MySQL 20 MySQL data directory

Mysql33 multi version concurrency control
![[C language foundation] 04 judgment and circulation](/img/59/4100971f15a1a9bf3527cbe181d868.jpg)
[C language foundation] 04 judgment and circulation

Adaptive Bezier curve network for real-time end-to-end text recognition
![[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP](/img/7d/8cbbd2f328a10808319458a96fa5ec.jpg)
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP

Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly

Generate PDM file from Navicat export table

MySQL23-存储引擎

Mysql34 other database logs
随机推荐
MySQL31-MySQL事务日志
Navicat 導出錶生成PDM文件
Why is MySQL still slow to query when indexing is used?
MySQL23-存儲引擎
[reading notes] rewards efficient and privacy preserving federated deep learning
MySQL33-多版本并发控制
Installation and use of MySQL under MySQL 19 Linux
Mysql21 user and permission management
Unicode decodeerror: 'UTF-8' codec can't decode byte 0xd0 in position 0 successfully resolved
Mysql30 transaction Basics
MySQL25-索引的创建与设计原则
CSDN问答标签技能树(五) —— 云原生技能树
Invalid global search in idea/pychar, etc. (win10)
Redis的基础使用
API learning of OpenGL (2005) gl_ MAX_ TEXTURE_ UNITS GL_ MAX_ TEXTURE_ IMAGE_ UNITS_ ARB
导入 SQL 时出现 Invalid default value for ‘create_time‘ 报错解决方法
35 is not a stumbling block in the career of programmers
MySQL27-索引優化與查詢優化
Water and rain condition monitoring reservoir water and rain condition online monitoring
MySQL transaction log