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
0
to10^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 ...
随机推荐
- sqlserver 行列转换
http://www.cnblogs.com/zhangzt/archive/2010/07/29/1787825.html PIVOT用于将列值旋转为列名(即行转列),在SQL Server 200 ...
- ASP.NET MVC 插件化机制
概述 nopCommerce的插件机制的核心是使用BuildManager.AddReferencedAssembly将使用Assembly.Load加载的插件程序集添加到应用程序域的引用中.具 体实 ...
- JVM相关
内存栅栏 1 what 说白了是一些cpu或编译器的一些同步指令 2 why cpu的cache快,而不去内存中取数据,在多线程并发中会读到cache的数据. 3 how 一般直接用关键字viloti ...
- Python学习笔记(一)——环境搭建
一.安装包下载: 国内镜像:32位:http://pan.baidu.com/s/1jI4q4lS 64位:http://pan.baidu.com/s/1eRPhpRW 版本更迭速度很 ...
- MIT 6.828 JOS学习笔记4. Lab 1 Part 2.1: The Boot Loader
Part 2: The Boot Loader 对于PC来说,软盘,硬盘都可以被划分为一个个大小为512字节的区域,叫做扇区.一个扇区是一次磁盘操作的最小粒度.每一次读取或者写入操作都必须是一个或多个 ...
- xcode下载
http://adcdownload.apple.com/Developer_Tools/Xcode_7.3.1/Xcode_7.3.1.dmg http://adcdownload.apple.co ...
- 2016 Multi-University Training Contest 5
6/12 2016 Multi-University Training Contest 5 期望+记忆化DP A ATM Mechine(BH) 题意: 去ATM取钱,已知存款在[0,K]范围内,每一 ...
- zorka源码解读之tracer内部实现
核心类: ZorkaAsyncThread.java protected BlockingQueue<T> submitQueue; /** * Processes single item ...
- [机器学习] ——KNN K-最邻近算法
KNN分类算法,是理论上比较成熟的方法,也是最简单的机器学习算法之一. 该方法的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别 ...
- Redis 数据类型总结—String
1.1 数据类型 Redis常用五种数据类型:string, hash, list, set, zset(sorted set). Redis内部使用一个redisObject对象来 ...