https://leetcode.com/problems/hamming-distance/

将两个二进制数比较,输出不同位数的个数

Input: x = 1, y = 4

Output: 2

Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑ 解题思路:
此题考察的是知识点是运算符,特别是异或运算符^
异或运算符作用为“相同出0,不同出1”,这个特性正好可以用来解题
那么正常的思路就是将x,y两数进行异或运算,然后统计1的出现次数 解法(Python):
 class Solution(object):
def hammingDistance(self, x, y):
return bin(x^y).count("");

这个解法其实很low的,首先用了bin()函数,作用是将异或结果二进制化为字符串,然后利用字符串函数count统计1出现的次数并输出

优秀解法评析(Java):

 public int hammingDistance(int x, int y) {
int xor = x ^ y, count = 0;
for (int i=0;i<32;i++) count += (xor >> i) & 1;
return count;
}

由于题目有限制0 ≤ x, y < 231

所以这个解法将异或结果一个个移位,然后和1和运算,自然相同为1,不同为0,然后用count器加上这个相同的1自然就是“1出现的次数”

461. Hamming Distance的更多相关文章

  1. LeetCode:461. Hamming Distance

    package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...

  2. Leetcode#461. Hamming Distance(汉明距离)

    题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...

  3. 【leetcode】461. Hamming Distance

    problem 461. Hamming Distance solution1: 根据题意,所求汉明距离指的是两个数字的二进制对应位不同的个数.对应位异或操作为1的累积和. class Solutio ...

  4. [Leetcode/Javascript] 461.Hamming Distance

    [Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...

  5. 461. Hamming Distance and 477. Total Hamming Distance in Python

    题目: The Hamming distance between two integers is the number of positions at which the corresponding ...

  6. 4. leetcode 461. Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  7. 461. Hamming Distance(leetcode)

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  8. LeetCode 461. Hamming Distance (汉明距离)

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  9. LeetCode 461 Hamming Distance 解题报告

    题目要求 The Hamming distance between two integers is the number of positions at which the corresponding ...

  10. [LeetCode&Python] Problem 461. Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

随机推荐

  1. Java Win自动环境配置脚本

    @echo off :: TODO:设置java环境变量 :: Author: Sunky color 02 set "NewPath=%cd%" set "JavaHo ...

  2. mui 动态加载数据出现的问题处理 (silder )

    mui-slider 问题:动态给mui的图片轮播添加图片,轮播不滚动. 解决:最后把滚动轮播图片的mui(".mui-slider").slider({interval: 300 ...

  3. python爬乌云dorps文章

    有时候翻看drops 无赖drops没有一个全部文章列表,所以就有了这个想法把所有文件标题链接都爬出来这样又直观又好找感兴趣的文章 #coding=utf-8 import re import url ...

  4. “(null)” is of a model that is not supported by this version of Xcode. Please use a different device.

    ios    真机运行程序就弹出这个"(null)" is of a model that is not supported by this version of Xcode. P ...

  5. css3 圆角

    -moz-border-radius: 15px; /* Gecko browsers */ -webkit-border-radius: 15px; /* Webkit browsers */ bo ...

  6. 移动端Web开发调试之Chrome远程调试(Remote Debugging)

    比如手机钉钉调试页面,下面是一位同学整理的链接: http://blog.csdn.net/freshlover/article/details/42528643/ 如果inspect 后,一直空白, ...

  7. mysql二级索引

    以InnoDB来说,每个InnoDB表具有一个特殊的索引称为聚集索引.如果您的表上定义有主键,该主键索引是聚集索引.如果你不定义为您的表的主键 时,MySQL取第一个唯一索引(unique)而且只含非 ...

  8. npm 发布到远程资源库

    一.npm 发布到远程资源库 1.创建package.json Package.json 属性说明 name - 包名. version - 包的版本号. description - 包的描述. ho ...

  9. java时间类型操作

    java中Date类型与字符串相互转化 Date date = new Date();        System.out.println(date);        //DateFormat类为一个 ...

  10. yum 操作复习

    RPM与YUM是配合使用的. rpm负责从网站或指定的文件路径,获取到rpm软件包.也就是说你要安装什么服务或软件,就要先找到rpm包,下载下来.也就是通常说的配置yum源. 啥是yum源.你下载下的 ...