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 ...
随机推荐
- hdu 1501 Zipper
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1501 思路:题目要求第三个串由前两个组成,且顺序不能够打乱,搜索大法好 #include<cstdi ...
- MongoDB的导入导出(7)
导入/导出可以操作的是本地的mongodb服务器,也可以是远程的. 所以,都有如下通用选项: -h host 主机 --port port 端口 -u username 用户名 -p pas ...
- SpringMVC学习(三)整合SpringMVC和MyBatis
工程结构 导入jar包 配置文件 applicationContext-dao.xml---配置数据源.SqlSessionFactory.mapper扫描器 applicationContext-s ...
- [译]:Orchard入门——导航与菜单
原文链接:Navigation and Menus 文章内容基于Orchard1.8版本.同时包含Orchard 1.5之前版本的导航参考 Orchard有许多不同的方法来创建菜单.本文将介绍两种较为 ...
- 配置Tomcat使用https协议
一. 创建tomcat证书 这里使用JDK自带的keytool工具来生成证书: 1. 在jdk的安装目录\bin\keytool.exe下打开keytool.exe 2. 在命令行中输入以下命令: ...
- 使用lnmp一键安装包后yum源出现的问题与解决
遇到一个问题就是执行 Yum update 或使用yum安装软件等命令的时候会有一个链接报404,使用find查找到该链接与kbsingh-CentOS-Extras.repo这个包有关,删除后yum ...
- 08 Servlet
Servlet技术 * Servlet开发动态的Web资源的技术. * Servlet技术 * 在javax. ...
- 数位DP BZOJ 1026 [SCOI2009]windy数
题目链接 前面全是0的情况特判 #include <bits/stdc++.h> int dp[10][10]; int digit[10]; int DFS(int pos, int v ...
- Is It A Tree?[HDU1325][PKU1308]
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP
[BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M< ...