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 ≤ xy < 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.

题意:找到两个数的汉明距离,即两个数转换成二进制后有多少位不相同。基本思路:两数直接按位异或,如某一位两数相同则为0,反之为1。然后变成了数异或结果有多少个1。查1的方法有很多,这里不是重点留到后面再说,这里面先使用一种最原始的。这道题没有复杂的内存分配,所以用c和c++写法是类似的,所以写了c。
 1 int hammingDistance(int x, int y) {
 2     int z = x ^ y;
 3     int cnt = 0;
 4     for( ; z > 0; )
 5     {
 6         if(z%2 == 1)//除2余数为1,就在统计结果上加1,这里是最通俗的想法,并不是最简洁写法
 7             cnt++;
 8         z /=2 ;//将z除2 ,统计下一位是否为1
 9     }
10     return cnt;
11 }

以上就是这道简单难度通过率最高的题的思路,运行时间3ms。

附上我写的最简写法。

1 int hammingDistance(int x, int y) {
2     int z = x ^ y,cnt=0;
3     for(int i=0;i<32;i++) cnt+=(z>>i)&1;
4     return cnt;
5 }
				
时间: 2024-08-03 15:23:43

LeetCode解题思路:461. Hamming Distance的相关文章

[解题报告]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

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

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:

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 (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

LeetCode解题思路:595. Big Countries

There is a table World +-----------------+------------+------------+--------------+---------------+ | name | continent | area | population | gdp | +-----------------+------------+------------+--------------+---------------+ | Afghanistan | Asia | 652

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【数学|位运算】

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的移位依次匹配是否对