[解题报告]461. Hamming Distance

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):
1 class Solution(object):
2     def hammingDistance(self, x, y):
3         return bin(x^y).count("1");

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

优秀解法评析(Java):

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

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

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

时间: 2024-10-03 14:00:48

[解题报告]461. Hamming Distance的相关文章

LeetCode解题思路:461. Hamming Distance

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

461. Hamming Distance

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 < 2^31. Example: Input: x = 1, y = 4 Output: 2 Explanation:

461. Hamming Distance(leetcode)

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 < 2^31. Example: Input: x = 1, y = 4 Output: 2 Explanation:

461. Hamming Distance【数学|位运算】

2017/3/14 15:23:55 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. 题目要求:求两个数字二进制位中同一位置不同bit的个数. 解法1  Java    利用1的移位依次匹配是否对

LeetCode 461. Hamming Distance

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

LeetCode 461. Hamming Distance (汉明距离)

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

Leetcode 461. Hamming Distance JAVA语言

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. PS:求海明距离. 思路:就是求x和y二进制的异或中的1的个数 public class Solution {     public int ham

LeetCode 461. Hamming Distance (C++)

题目: 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 < 2^31. Example: Input: x = 1, y = 4 Output: 2 Explanati

461. Hamming Distance - 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