LeetCode Total Hamming Distance
原题链接在这里:https://leetcode.com/problems/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:
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.
Note:
- Elements of the given array are in the range of
0to10^9 - Length of the array will not exceed
10^4.
题解:
对于int的32位的每一位,计算在这一位是1的array element个数k, 那么在这一位是0的个数就是nums.length-k. 对于这一位就有k*(nums.length-k)种选法可以产生Hamming Distance. 然后将32位Hamming Distance 个数相加就是总数.
Time Complexity: O(1).
Space: O(1).
AC Java:
public class Solution {
public int totalHammingDistance(int[] nums) {
int res = 0;
int len = nums.length;
for(int i = 0; i< 32; i++){
int bitCount = 0;
for(int j = 0; j<len; j++){
bitCount += (nums[j] >> i) & 1;
}
res += bitCount*(len - bitCount);
}
return res;
}
}
LeetCode Total Hamming Distance的更多相关文章
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...
- [LeetCode] 477. Total Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...
- [Swift]LeetCode477. 汉明距离总和 | Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 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 ...
- 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 ...
- LeetCode 461. Hamming Distance (汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
随机推荐
- python之常用内置函数
python内置函数,可以通过python的帮助文档 Build-in Functions,在终端交互下可以通过命令查看 >>> dir("__builtins__&quo ...
- SQL Server 2012 新增语法
--连接两个字符串. CONCAT(TelePhone,UserName,' : ',LoginVCode) FROM [dbo].[TB_NUsers] --SQL Server2012新增了两个逻 ...
- 第01章(认识Java)
/***************** ***认识java第一章 *******知识点: **************1.开发环境搭建 **************2.开发工具使用 ********** ...
- 巴特沃斯(Butterworth)滤波器 (1)
下面深入浅出讲一下Butterworth原理及其代码编写. 1. 首先考虑一个归一化的低通滤波器(截止频率是1),其幅度公式如下: 当n->∞时,得到一个理想的低通滤波反馈: ω<1时,增 ...
- [R语言]R语言使用多线程对数据库进行大批量访问时出现无法连接问题
问题描述: 在R中使用多线程对数据库进行写入,在服务器端运行脚本(linux环境),总是在第6-7万个任务线程时,出现无法连接到数据库的问题.任务中断,错误信息为task 6xxxx failed,C ...
- 转:AngularJS的Filter用法详解
Filter简介 Filter是用来格式化数据用的. Filter的基本原型( '|' 类似于Linux中的管道模式): {{ expression | filter }} Filter可以被链式使用 ...
- tornado 学习笔记10 Web应用中模板(Template)的工作流程分析
第8,9节中,我们分析Tornado模板系统的语法.使用以及源代码中涉及到的相关类,而且对相关的源代码进行了分析.那么,在一个真正的Web应用程序中,模板到底是怎样使用?怎样被渲染? ...
- xp系统重绘边框线不显示(首次加载没有触发paint事件)
同样是,重绘边框事件,win7系统显示正常,而xp系统却不显示,这是什么原因造成的呢? 于是,小编开始百度,不停的查找原因,通过一番查找,小编也意外的收获了一些内容: 例如:窗口的拖动,放大,缩小,等 ...
- vim_cfg
set nocompatible set langmenu=en_US let $LANG = 'en_US' source $VIMRUNTIME/delmenu.vim source $VIMRU ...
- R爬虫知识点
>>如何用 R 模仿浏览器的行为? GET / POST URLencode / URLdecode (破解中文網址的祕密) header & cookie 如何突破使用 cook ...