477:汉明距离总和
This commit is contained in:
parent
44447da0aa
commit
becdd62d1d
54
src/main/java/leetcode/editor/cn/TotalHammingDistance.java
Normal file
54
src/main/java/leetcode/editor/cn/TotalHammingDistance.java
Normal file
@ -0,0 +1,54 @@
|
||||
//两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量。
|
||||
//
|
||||
// 计算一个数组中,任意两个数之间汉明距离的总和。
|
||||
//
|
||||
// 示例:
|
||||
//
|
||||
//
|
||||
//输入: 4, 14, 2
|
||||
//
|
||||
//输出: 6
|
||||
//
|
||||
//解释: 在二进制表示中,4表示为0100,14表示为1110,2表示为0010。(这样表示是为了体现后四位之间关系)
|
||||
//所以答案为:
|
||||
//HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 +
|
||||
//2 + 2 = 6.
|
||||
//
|
||||
//
|
||||
// 注意:
|
||||
//
|
||||
//
|
||||
// 数组中元素的范围为从 0到 10^9。
|
||||
// 数组的长度不超过 10^4。
|
||||
//
|
||||
// Related Topics 位运算
|
||||
// 👍 154 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
//477:汉明距离总和
|
||||
public class TotalHammingDistance{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new TotalHammingDistance().new Solution();
|
||||
System.out.println(solution.totalHammingDistance(new int[]{4, 14, 2}));
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int totalHammingDistance(int[] nums) {
|
||||
int length = nums.length;
|
||||
int result = 0;
|
||||
int size = 30;
|
||||
for (int i = 0; i < size; ++i) {
|
||||
int count = 0;
|
||||
for (int num : nums) {
|
||||
count += (num >> i) & 1;
|
||||
}
|
||||
result += count * (length - count);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
23
src/main/java/leetcode/editor/cn/TotalHammingDistance.md
Normal file
23
src/main/java/leetcode/editor/cn/TotalHammingDistance.md
Normal file
@ -0,0 +1,23 @@
|
||||
<p>两个整数的 <a href="https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E8%B7%9D%E7%A6%BB/475174?fr=aladdin">汉明距离</a> 指的是这两个数字的二进制数对应位不同的数量。</p>
|
||||
|
||||
<p>计算一个数组中,任意两个数之间汉明距离的总和。</p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> 4, 14, 2
|
||||
|
||||
<strong>输出:</strong> 6
|
||||
|
||||
<strong>解释:</strong> 在二进制表示中,4表示为0100,14表示为1110,2表示为0010。(这样表示是为了体现后四位之间关系)
|
||||
所以答案为:
|
||||
HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.
|
||||
</pre>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li>数组中元素的范围为从 <code>0</code>到 <code>10^9</code>。</li>
|
||||
<li>数组的长度不超过 <code>10^4</code>。</li>
|
||||
</ol>
|
||||
<div><div>Related Topics</div><div><li>位运算</li></div></div>\n<div><li>👍 154</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user