461. 汉明距离(Hamming Distance)leetcode
首先附上题目链接: https://leetcode-cn.com/problems/hamming-distance/
一:题目
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。给出两个整数 x 和 y,计算它们之间的汉明距离。
注意:0 ≤ x, y < 231.
示例:
输入: x = 1, y = 4
输出: 2
解释:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑
上面的箭头指出了对应二进制位不同的位置。
简单说明:这里就是要我们求两个数对应的二进制,有多少个位置的1是单独的,这里的单独意思是指两个数在某个位置是(0,1)形式存在,那这个1就是单独的。
需要注意的是它给的x,y的范围,所以我们的变量只需要int就行了。
二:方法(c++)
1:方法一:直接移位,每次取两个的最后一位进行判断
这种二进制的题目,首先想到的就是能不能移位和亦或,或,与等操作。这里将每个数的最后一位比较,若是一个为0,一个为1则计数。
int count=;
while(x!=||y!=)
{
if((x&)!=(y&))
{
count++;
}
x>>=;
y>>=;
}
return count ;
2:方法二:汉明重量加汉明距离
这里首先需要介绍这两个概念。汉明重量:某个数二进制中1的数量。 汉明距离:二进制位不同的位置的数目(就如题目所说,对应1的位置不同的数目)。
汉明重量:n=n&(n-1)
这里以6为例子:0110
第一次;0110&0101=0100 :将最后一个1去掉了
第二次:0100&0011=000 :将最后一位去掉了
汉明距离:本质为两个数异或后字符"1"的个数,可以直接使用异或实现。
所以这里我们首先异或算出哪些地方不同位置1。然后使用汉明重量,一次次统计异或得出的二进制里面1的个数。
int hammingDistance(int x, int y) {
//汉明距离加重量
int sum=x^y;// 异或来判断哪些不相同的,待会就是把不相同的一个个判别出来
int count=;
while(sum!=)
{
sum=sum&sum-;//依次去掉一
count++;
}
return count;
}
461. 汉明距离(Hamming Distance)leetcode的更多相关文章
- [Swift]LeetCode461. 汉明距离 | Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 【LeetCode】汉明距离(Hamming Distance)
这道题是LeetCode里的第461道题. 题目描述: 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, ...
- 【leetcode 461】. Hamming Distance
要求: 给定两个整数x和y,0 ≤ x, y < 231. 求x和y的汉明距离. Example: Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 ...
- Leetcode#461. Hamming Distance(汉明距离)
题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...
- [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] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 【leetcode】461. Hamming Distance
problem 461. Hamming Distance solution1: 根据题意,所求汉明距离指的是两个数字的二进制对应位不同的个数.对应位异或操作为1的累积和. class Solutio ...
- [Leetcode/Javascript] 461.Hamming Distance
[Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...
随机推荐
- es6 模块编译 *** is not function
今天学习vuejs,里面用到了es6的写法,遇到了一个很怪的问题,不知道有人遇到么. 安装的模块引用:import Vue from 'vue';(注意,Vue处没有{},如果加上这个就报错Uncau ...
- Java-IPDeal工具类
/** * IP处理类 */ import java.sql.*; public class IPDeal { /** * 通过ip地址查询地区名称 * @param strip * @return ...
- ubuntu设置窗口最大化
三行命令搞定Ubuntu 16.04下安装VMware Tools!!!!!!!!! 由于下载的是ubuntu-16.04.3-desktop-amd64,需要安装vmware tools,以往提取的 ...
- apache log4j将日志保存在mongodb数据库中(转)
og4j与mongodb整合 Mongo Java driver jar包 log4mongo-java jar包 配置log4j.properties文件,使之整合mongodb: #将Mongod ...
- Educational Codeforces Round 26 [ D. Round Subset ] [ E. Vasya's Function ] [ F. Prefix Sums ]
PROBLEM D - Round Subset 题 OvO http://codeforces.com/contest/837/problem/D 837D 解 DP, dp[i][j]代表已经选择 ...
- ipvsadm命令用法
ipvsadm命令选项 -A 添加虚拟服务器 -E 修改虚拟服务器 -D ...
- BZOJ 1951: [Sdoi2010]古代猪文 ExCRT+欧拉定理+Lucas
欧拉定理不要忘记!! #include <bits/stdc++.h> #define N 100000 #define ll long long #define ull unsigned ...
- Python和Sublime的整合
按照下面的方式也可以运行py代码,但是交互方式不是很好,主要简单介绍了一下Python和Sublime的整合 一 Ctrl + B 二 SublimeREPL 安装SublimeREPL 1.打开P ...
- 洛谷P3193 GT考试 kmp+矩阵优化dp
题意 求\(N\)位数字序列(可以有前导0)中不出现某\(M\)位子串的个数,模\(K\). \(N<=10^9,M<=20,K<=1000\) 分析 设\(dp[i][j]\)表示 ...
- 深度学习笔记(十一)网络 Inception, Xception, MobileNet, ShuffeNet, ResNeXt, SqueezeNet, EfficientNet, MixConv
1. Abstract 本文旨在简单介绍下各种轻量级网络,纳尼?!好吧,不限于轻量级 2. Introduction 2.1 Inception 在最初的版本 Inception/GoogleNet, ...