【LeetCode】汉明距离(Hamming Distance)
这道题是LeetCode里的第461道题。
题目描述:
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
给出两个整数
x和y,计算它们之间的汉明距离。注意:
0 ≤x,y< 231.示例:
输入: x = 1, y = 4 输出: 2 解释:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑ 上面的箭头指出了对应二进制位不同的位置。
题目都暗示了使用位运算,然后把结果转换成二进制数。
解题代码:
class Solution {
public int hammingDistance(int x, int y) {
int c,res = 0;
c = x ^ y;//异或位运算
while(c != 0) {
if(c % 2 == 1) {//二进制转换
res++;
}
c/=2;
}
return res;
}
}
提交结果:

个人总结:
普及一下汉明码吧:汉明的基本操作是异或运算。汉明码可以用来检测转移数据是发生的错误,并修正错误。如:QQ上有人给你发了条消息 "0 1111",其中汉明码为 "0" 在开头,发送中途信号被干扰了,变成了 "1 1110",其中汉明码为 "1" 在开头由 "0" 变成了 "1",这个时候汉明码就起作用了,但是这只是仅仅知道了错误,但却不能纠错,想要纠错只能再添加一位汉明码。具体的东西还是太深奥了,感兴趣想了解的自行百度。
【LeetCode】汉明距离(Hamming Distance)的更多相关文章
- [LeetCode] Total Hamming Distance 全部汉明距离
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 (汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- Leetcode#461. Hamming Distance(汉明距离)
题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...
- [Swift]LeetCode461. 汉明距离 | Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- LeetCode Total Hamming Distance
原题链接在这里:https://leetcode.com/problems/total-hamming-distance/ 题目: The Hamming distance between two i ...
- [LeetCode] 461. Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...
- LeetCode:461. Hamming Distance
package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...
- 4. leetcode 461. Hamming Distance
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 ...
随机推荐
- python 之开发工具 sublimetext 3
一.前言 由于个人工作内容太过于繁杂,记忆力又不好,为日后使用的方便,故简单的记录了本篇关于sublimetext 3的初始化安装和部分插件内容的记录.目前最新的版本也是3.0以上版本了,故我这里使用 ...
- Js面向对象之观察者模式
//模拟一个目标可能拥有的一些列依赖 function ObserverList() { this.observerList = []; }; //添加一个观察者 ObserverList.proto ...
- Altera FFT核使用详解
简介 快速傅里叶变换(Fast Fourier Transform)最为一种高效的算法,被广泛的用于信号处理与数据分析等领域.对于设计工程师来讲,自己动手采样可编程语言来实现一个FFT/IFFT模块, ...
- iOS 常用正则表达式
今天看到一个正则表达式的文章,总结的挺好的,就自己转载一下,我还会陆续加入一些我自己看到常用的正则表达式 (原地址:http://www.code4app.com/blog-721976-112.ht ...
- 用ComboBox控件制作浏览器网址输入框
实现效果: 知识运用: ComboBox控件的FindString public int FindString(string s) //查找数据项集合中指定数据项的索引 和Select方法 publi ...
- AngularJs学习笔记-表单处理
表单处理 (1)Angular表单API 1.模板式表单,需引入FormsModule 2.响应式表单,需引入ReactiveFormsModule (2)模板式表单 在Angular中使用for ...
- ERR_FAILED 浏览器访问
我是针对上一篇的问题 继续的探究 ,我百度了 看了这 https://zhidao.baidu.com/question/1175643597811783659.html 之后我就用 电脑管家进行系 ...
- RabbitMQ使用教程(五)如何保证队列里的消息99.99%被消费?
1. 前情回顾 RabbitMQ使用教程(一)RabbitMQ环境安装配置及Hello World示例 RabbitMQ使用教程(二)RabbitMQ用户管理,角色管理及权限设置 RabbitMQ使用 ...
- SummerVocation_Learning--java的自动打包与解包
Auto Boxing: 自动将基础类型转换成对象(JDK1.5之后支持) Auto UnBoxing:自动将对象转换成基础类型 如 Map中的put方法,如果要传入键值对<a,1>,&l ...
- 【Python学习之十】yield之send方法
yield作用 简单地讲,yield 的作用就是把一个函数变成一个 generator,带有 yield 的函数不再是一个普通函数,Python 解释器会将其视为一个 generator.下面以斐波拉 ...