Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Now your job is to find the total Hamming distance between all pairs of the given numbers.
Example:
Input: 4, 14, 2 Output: 6 Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just
showing the four bits relevant in this case). So the answer will be:
HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6. 这题有个关键点,如果有n个数,要么是1要么是0,那么如果里面有k个1,那么Hamming distance = k * (n - k)
public class Solution {
public int totalHammingDistance(int[] nums) {
int total = , n = nums.length;
for (int j = ; j < ; j++) {
int bitCount = ;
for (int i = ; i < n; i++) {
bitCount += (nums[i] >> j) & ;
}
total += bitCount * (n - bitCount);
}
return total;
}
}
Total Hamming Distance的更多相关文章
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- LeetCode Total Hamming Distance
原题链接在这里:https://leetcode.com/problems/total-hamming-distance/ 题目: The Hamming distance between two i ...
- [Swift]LeetCode477. 汉明距离总和 | Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 477. Total Hamming Distance总的二进制距离
[抄题]: The Hamming distance between two integers is the number of positions at which the correspondin ...
- [LeetCode] 477. Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] 477. Total Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...
- 【LeetCode】477. Total Hamming Distance 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...
- 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 ...
- 461. Hamming Distance + 477. Total Hamming Distance
▶ 与 Hamming 距离相关的两道题. ▶ 461. 求两个数 x 与 y 的哈夫曼距离. ● 代码,4 ms,对 x 和 y 使用异或,然后求值为 1 的位的个数. class Solutio ...
随机推荐
- .Net mvc 根据前台参数动态绑定对象
业务需求:根据前台界面的参数,动态绑定对象 <param name="colNames">属性名拼接字符串</param><param name=&q ...
- iOS 设置不同的字体颜色
//设置不同字体颜色 -(void)fuwenbenLabel:(UILabel *)labell FontNumber:(UIFont *)font AndRange:(NSRange)range ...
- caffe源码解析
http://blog.csdn.net/lanxuecc/article/details/53186613
- C#图像处理笔记
1.灰度拉伸 灰度拉伸又叫对比度拉伸,它是最基本的一种灰度变换,使用的是最简单的分段线性变换函数,它的主要思想是提高图像处理时灰度级的动态范围.
- cocos2dx3.0的CCCallFunc、CCCallFuncN
来源: http://blog.csdn.net/crayondeng/article/details/18767407 二.在cocos2d-x中,还有一个地方是需要大量使用到回调函数的,这就是回调 ...
- C#面向对象设计模式纵横谈——2.Singleton 单件(创建型模式)
一:模式分类 从目的来看: 创建型(Creational)模式:负责对象创建. 结构型(Structural)模式:处理类与对象间的组合. 行为型(Behavioral)模式:类与对象交互中的职责分配 ...
- 【codeforces 148D】 Bag of mice
http://codeforces.com/problemset/problem/148/D (题目链接) 题意 包中有w个白鼠,b个黑鼠.公主和龙轮流画老鼠,公主先画,谁先画到白鼠谁就赢.龙每画完一 ...
- 10个常见的Node.js面试题
如果你希望找一份有关Node.js的工作,但又不知道从哪里入手评测自己对Node.js的掌握程度. 本文就为你罗列了10个常见的Node.js面试题,分别考察了Node.js编程相关的几个主要方面. ...
- iOS - 跳到系统App内部设置
从App中跳转到手机设置中此App内的设置授权界面: NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ...
- Java使用Fork/Join框架来并行执行任务
现代的计算机已经向多CPU方向发展,即使是普通的PC,甚至现在的智能手机.多核处理器已被广泛应用.在未来,处理器的核心数将会发展的越来越多. 虽然硬件上的多核CPU已经十分成熟,但是很多应用程序并未这 ...