Files
2020-08-09 00:39:24 +08:00

37 lines
1.1 KiB
Markdown
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [461. Hamming Distance](https://leetcode.com/problems/hamming-distance/)
## 题目
The [Hamming distance](https://en.wikipedia.org/wiki/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.
## 题目大意
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目给出两个整数 x y计算它们之间的汉明距离
注意
0 x, y < 231.
## 解题思路
- 2 个数的海明距离海明距离的定义是两个数二进制位不同的总个数这一题利用的位操作的是 X &= (X - 1) 不断的清除最低位的 1 先将这两个数异或异或以后清除低位的 1 就是最终答案