LeetCode之Hamming Distance
1、题目
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x
and y
, calculate the Hamming distance.
Note:
0 ≤ x
, y
< 231.
Example:
Input: x = 1, y = 4Output: 2Explanation:
1 (0 0 0 1)
4 (0 1 0 0)↑ ↑The above arrows point to positions where the corresponding bits are different.
题意:
给定两个整数x,y,计算x和y的汉明距离。汉明距离是指x、y的二进制表示中,相同位置上数字不相同的所有情况数。
2、代码实现
public class Solution {public int hammingDistance(int x, int y) {int notSame = x ^ y;int count = 0;while (notSame != 0) {if (notSame % 2 == 1)++count;notSame /= 2;}return count;}
}
注意记得用 异或
0 ^ 1 = 1
LeetCode之Hamming Distance相关推荐
- Leetcode 461. Hamming Distance JAVA语言
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] 461. Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- LeetCode 461 Hamming Distance(汉明距离)
Q:The Hamming distance between two integers is the number of positions at which the corresponding bi ...
- LeetCode 461. Hamming Distance
题目: The Hamming distance between two integers is the number of positions at which the corresponding ...
- 【LeetCode】461. Hamming Distance (java实现)
2019独角兽企业重金招聘Python工程师标准>>> 原题链接 https://leetcode.com/problems/hamming-distance/ 原题 The Ham ...
- LeetCode(191 461):位1的个数 Number of 1 Bits 汉明距离 Hamming Distance(Java)
2019.9.14 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) github:https://github.com/ChopinXBP/LeetCode-Babel 之前在 ...
- C#LeetCode刷题之#461-汉明距离(Hamming Distance)
问题 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意:0 ≤ x, y < 231. 输入: x = 1, y = ...
- Leetcode 477. Total Hamming Distance 总海明距离 解题报告
1 解题思想 题目的意思就是给了一个数组,现在求总的海明距离,其中: 1.海明距离:任意两个数在二级制的表示下(int = 32bit),每个bit对应的值是1或0,那么这两个数在这32个位置下,取值 ...
- LeetCode 613. Shortest Distance in a Line --SQL
LeetCode 613. Shortest Distance in a Line --SQL LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专栏里,大部 ...
最新文章
- iBATIS配置文件的特殊使用方法
- [手机分享]黑莓手机7系列分享之——黑莓7730
- Selenium之Android使用学习
- 波卡链Substrate (4)托盘Pallets
- Shell编程之case语句
- 【TensorFlow】TensorFlow函数精讲之tf.train.ExponentialMovingAverage()
- html和vue的区别,2018-09-16第四课 (v-html和v-text的区别,v-once和v-pre的区别,vue的生命周期,选项卡)...
- T4 Templates and the Entity Framework
- byte二维数组表示
- 我一口气发现7个Safari 0day,苹果奖了7.5万美元
- 坚守本心,你公司的OA解决了以下问题吗?
- FJUT 借教室 (线段树区间查询+区间修改)
- html鼠标平滑滚动效果,JS特效代码 JS动态平滑滚动菜单效果
- 金蝶云星空API调用实践
- 图形界面上的任意形状图形按钮
- 进程监控工具 Procmon有Linux版本了
- android 关机闹钟 实现,android 关机闹钟
- postgres 判断null_PostgreSQL空值null参与运算的处理方法
- linux sed 目录递归,shell递归遍历目录下的所有文件并统一改名的方法-文件更名...
- iOS 屏幕录制 (可选录入麦克风的声音)
热门文章
- 自定义EventSource(二)PollingCounter
- 面向.NET开发人员的Dapr——参考应用程序
- 用NSubstitute来mock
- WPF 制作便携小空调
- 聊一聊和Nacos 2.0.0对接那些事
- 欢迎来到 C# 9.0(Welcome to C# 9.0)
- Dotnet core基于ML.net的销售数据预测实践
- .NET Core + Kubernetes:快速体验
- .NET Core开发实战(第23课:静态文件中间件:前后端分离开发合并部署骚操作)--学习笔记(上)...
- 【朝夕技术专刊】Core3.1WebApi_Filter多种注册方式支持依赖注入