当前位置:网站首页>Leetcode Hamming distance simple
Leetcode Hamming distance simple
2022-06-13 05:49:00 【AnWenRen】
title :461 Hamming distance - Simple
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
xandy, Calculate and return the Hamming distance between them .
Example 1
Input :x = 1, y = 4
Output :2
explain :
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑
The arrows above indicate different positions of corresponding binary bits .
Example 2
Input :x = 3, y = 1
Output :1
Tips
0 <= x, y <= 231 - 1
Code Java
public int hammingDistance(int x, int y) {
// obtain x And y Different locations 1 The number of
int ones = x ^ y;
// Calculation ones in 1 The number of
int sum = 0;
while (ones > 0) {
ones &= ones - 1; // Clear low order 1
sum ++;
}
return sum;
}
边栏推荐
- Celery understands
- Difference between deviation and variance in deep learning
- Comment procéder à l'évaluation des algorithmes
- Leetcode- intersection of two arrays - simple
- Unity game optimization (version 2) learning record 7
- MySQL installation in Linux Environment
- 设置自定义dialog的正确宽高
- How to Algorithm Evaluation Methods
- Problems encountered in the use of PgSQL
- Why do so many people hate a-spice
猜你喜欢

Zero copy technology

Browser screenshot method (long screenshot, node screenshot, designated area screenshot)

Feel the power of shardingsphere JDBC through the demo

Four shardingsphere JDBC sharding strategies

18 flowable task manualtask and receivetask

Information collection for network security (2)

17 servicetask of flowable task

Mongodb multi field aggregation group by

1 Introduction to drools rule engine (usage scenarios and advantages)

Comment procéder à l'évaluation des algorithmes
随机推荐
A fast week
OpenGL馬賽克(八)
2021-9-19
MySQL performs an inner join on query. The query result is incorrect because the associated fields have different field types.
Getclassloader() returns null, getclassloader() gets null
18 flowable task manualtask and receivetask
Validation set: ‘flowable-executable-process‘ | Problem: ‘flowable-servicetask-missing-implementatio
C calls the API and parses the returned JSON string
Concurrent programming -- countdownlatch combined with thread pool
NVIDIA Jetson nano/xavier NX capacity expansion tutorial
[China & some provinces and cities] JSON file for offline map visualization
Mysql database backup and restore:
Error: unmapped character encoding GBK
Vagrant virtual machine installation, disk expansion and LAN access tutorial
powershell优化之一:提示符美化
arrayList && linkedList
使用cmake交叉编译helloworld
Solve the problem of garbled code in the MySQL execution SQL script database in docker (no need to rebuild the container)
2020 personal annual summary
Why do so many people hate a-spice