12.Hamming Distance(汉明距离)
Level:
Easy
题目描述:
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 = 4
Output: 2
Explanation:
1   (0 0 0 1)
4   (0 1 0 0)
       ↑   ↑
The above arrows point to positions where the corresponding bits are different.
思路分析:
汉明距离指的是两个数的二进制形式中对应位数值不同的数目。因此我们将两个数相异或,那么异或结果的二进制形式中非零位的个数就是汉明距离。
代码:
class Solution {
    public int hammingDistance(int x, int y) {
        int xor=(x^y);
        int t=1;
        int res=0;
        while(t>0){
            if((xor&t)!=0)
                res++;
            t<<=1;
        }
        return res;
    }
}
												
											12.Hamming Distance(汉明距离)的更多相关文章
- [LeetCode] 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 ...
 - 461. Hamming Distance(汉明距离)
		
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
 - 477 Total Hamming Distance 汉明距离总和
		
两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量.计算一个数组中,任意两个数之间汉明距离的总和.示例:输入: 4, 14, 2输出: 6解释: 在二进制表示中,4表示为0100,14表 ...
 - [LeetCode] Total Hamming Distance 全部汉明距离
		
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
 - [Swift]LeetCode461. 汉明距离 | Hamming Distance
		
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
 - [Swift]LeetCode477. 汉明距离总和 | Total 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 = ...
 - [LeetCode] 477. Total Hamming Distance 全部汉明距离
		
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
 
随机推荐
- PhoneGap打Android包报错
			
1.下载AndroidSDK,安装 2.下载Phonegap,解压,为以后打包用 3.下载Node.js,安装 4.下载并安装Ant工具 5.配置环境变量 ANT_HOME=ANT主目录路径 PATH ...
 - vue-cli脚手架build目录中的webpack.dev.conf.js配置文件
			
此文章用来解释vue-cli脚手架build目录中的webpack.dev.conf.js配置文件 此配置文件是vue开发环境的wepack相关配置文件 关于注释 当涉及到较复杂的解释我将通过标识的方 ...
 - js中substring和substr用法与区别
			
String.substring( ):用于返回一个字符串的子串 用法如下:string.substring(from, to) 其中from指代要抽去的子串第一个字符在原字符串中的位置 to指代所要 ...
 - django的render的说明
			
return render(request,"homesite.html",locals()) homesite.html页面中的所有内容都可以被渲染,不论是标签还是js代码,包括 ...
 - C#操作JSON专题
			
第一章:C#如何拿到从http上返回JSON数据? 第二章:C#如何解析JSON数据?(反序列化对象) 第三章:C#如何生成JSON字符串?(序列化对象) 第四章:C#如何生成JSON字符串提交给接口 ...
 - C程序设计语言(K&R) 笔记2
			
(1) #include <stdio.h> main(){ int c; while((c = getchar()) != EOF){ putchar(c); } } 注意,因为 != ...
 - php异步执行函数
			
1.在unix系统中,使用popen和pclose可以创建管道(通信途径)来连接到其他程序. 2.能够执行服务器命令的php函数有: exec(commond,$output) 接收一个命令,把得 ...
 - ASCII / Unicode / UTF-8 / GBK
			
1 ASCII ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现 ...
 - 电脑安装unity3d有C盘逐渐爆满问题解决方案
			
打开unity3d软件,Edit - Preference - GI Cache选中 Custom Cache Location,切换到别的盘,然后点击 Clean Cache清空一次,再查看C盘,存 ...
 - EZOJ #80
			
传送门 分析 经典的树型DP 我们记录dp[i][0/1]表示i的子树中到i的长度分别为偶数和奇数的长度和 dp2[i][0/1]则表示不在i的子树中的点到i的长度分别为偶数和奇数的长度和 然后根据边 ...