原题链接在这里: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:

  1. Elements of the given array are in the range of to 10^9
  2. 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;
}
}

类似Hamming Distance.

LeetCode Total Hamming Distance的更多相关文章

  1. [LeetCode] Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  2. [LeetCode] 477. Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  3. 【LeetCode】477. Total Hamming Distance 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...

  4. [LeetCode] 477. Total Hamming Distance(位操作)

    传送门 Description The Hamming distance between two integers is the number of positions at which the co ...

  5. [Swift]LeetCode477. 汉明距离总和 | Total Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  6. Total Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  7. 477. Total Hamming Distance总的二进制距离

    [抄题]: The Hamming distance between two integers is the number of positions at which the correspondin ...

  8. 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 ...

  9. LeetCode 461. Hamming Distance (汉明距离)

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

随机推荐

  1. SQL优化技巧

    我们开发的大部分软件,其基本业务流程都是:采集数据→将数据存储到数据库中→根据业务需求查询相应数据→对数据进行处理→传给前台展示.对整个流程进行分析,可以发现软件大部分的操作时间消耗都花在了数据库相关 ...

  2. 3.通过现有的PDB创建一个新的PDB

    实验说明:创建PDB除了可以通过种子PDB创建外,现在测试通过一个现有的用户PDB克隆创建新的PDB数据库 实验步骤: 1.创建测试数据 SQL> alter session set conta ...

  3. Class.forName()的作用

    大家都用过Class.forName(),也都知道是类加载的作用,其实这方法不只是类加载,还有类初始化. 下面用个小例子说明一下: A类,是用来加载的类 /** * 用来测试类加载的类此类有 * 静态 ...

  4. ubuntu SSH 连接、远程上传下载文件

    安装 SSH(Secure Shell) 服务以提供远程管理服务 sudo apt-get install ssh SSH 远程登入 Ubuntu 机 ssh username@192.168.0.1 ...

  5. oracle 资源学习汇总

    1:修改密码   http://blog.csdn.net/qh_java/article/details/23202259 2:Oracle数据库.实例.用户.表空间.表之间的关系

  6. iOS--NSTimer设置定时器的两种方法

    //方法一: //创建定时器 NSTimer *timer=[NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(next ...

  7. web api :Action Results in Web API 2

    原文:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/action-results Web api 返回 ...

  8. dbms_output.put_line长度限制问题

    dbms_output.put_line长度限制问题 对于10g以上版本(包括10g), dbms_output.put_line的最大长度限制是32767. 如果报错buffer overflow, ...

  9. vs发布的程序不依赖运行时库msvcp100.dll

      [摘要:msvcr100.dll:MS Visual C Runtime 100 msvcp100.dll:MS Visual CPp 100 vs建立的工程,运转时库的范例有MD(MDd)战MT ...

  10. VB下对HTML元素的操作

    <!DOCTYPE html> <html> <head> <title>test</title> </head> <bo ...