477. Total Hamming Distance
来源:互联网 发布:php html5 关系 编辑:程序博客网 时间:2024/06/02 04:20
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, 2Output: 6Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (justshowing 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
.
public class Solution { public int totalHammingDistance(int[] nums) { int count = 0, n = nums.length; for (int i = 0; i < 31; i ++) { int bitCount = 0; for (int num: nums) { bitCount += (num >>> i) & 1; } count += bitCount * (n - bitCount); } return count; }}
阅读全文
0 0
- 477. Total Hamming Distance
- 477. Total Hamming Distance
- 477. Total Hamming Distance
- 477. Total Hamming Distance**
- 477. Total Hamming Distance
- 477. Total Hamming Distance
- 477. Total Hamming Distance
- 477. Total Hamming Distance
- 477. Total Hamming Distance
- 477. Total Hamming Distance
- 477. Total Hamming Distance
- 477. Total Hamming Distance
- LeetCode 477. Total Hamming Distance
- 【Leetcode】477. Total Hamming Distance
- LeetCode 477. Total Hamming Distance
- [leetcode] 477. Total Hamming Distance
- LeetCode 477. Total Hamming Distance
- LeetCode 477. Total Hamming Distance
- git
- [FFT] [HDU5307] He is Flying
- Kali Linux安装之Kali和Windows双引导(转载)
- 用Mini ISO通过网络安装Kali Linux
- <c:forEach varStatus="status">中 varStatus的作用
- 477. Total Hamming Distance
- TProfiler简介
- linux下RPM包制作
- leetcode.496. Next Greater Element I
- 人机大战结局,为何日本公众的看法与我们不同?
- RPM包快速制作工具fpm的安装和使用
- Android自定义View的三种实现方式
- php tp框架电商项目防表单重复提交
- GitHub完美适配Visual Studio-01 简介