两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量。
计算一个数组中,任意两个数之间汉明距离的总和。
示例:
输入: 4, 14, 2
输出: 6
解释: 在二进制表示中,4表示为0100,14表示为1110,2表示为0010。(这样表示是为了体现后四位之间关系)
所以答案为:
HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.
注意:
    数组中元素的范围为从 0到 10^9。
    数组的长度不超过 10^4。
详见:https://leetcode.com/problems/total-hamming-distance/description/

C++:

class Solution {
public:
int totalHammingDistance(vector<int>& nums) {
int res=0,n=nums.size();
for(int i=0;i<32;++i)
{
int cnt=0;
for(int num:nums)
{
if(num&(1<<i))
{
++cnt;
}
}
res+=cnt*(n-cnt);
}
return res;
}
};

参考:http://www.cnblogs.com/grandyang/p/6208062.html

477 Total Hamming Distance 汉明距离总和的更多相关文章

  1. [LeetCode] 477. 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(位操作)

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

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

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

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

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

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

  6. 461. Hamming Distance + 477. Total Hamming Distance

    ▶ 与 Hamming  距离相关的两道题. ▶ 461. 求两个数 x 与 y 的哈夫曼距离. ● 代码,4 ms,对 x 和 y 使用异或,然后求值为 1 的位的个数. class Solutio ...

  7. LeetCode "477. Total Hamming Distance"

    Fun one.. the punch line of this problem is quite common in Bit related problems on HackerRank - vis ...

  8. 477. Total Hamming Distance

    class Solution { public: int totalHammingDistance(vector<int>& nums) { ; ; i < ; i++) { ...

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

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

随机推荐

  1. 教你如何配置Ubuntu用于高效、高质量的发送邮件

    本文首发在: http://mengxi.me/how-to-setup-ubuntu-sendmail-to-deliver-email-fast-and-reliable/ 在网站上线后,经常会遇 ...

  2. React创建组件的三种方式比较和入门实例

    推荐文章: https://www.cnblogs.com/wonyun/p/5930333.html 创建组件的方式主要有: 1.function 方式 2.class App extends Re ...

  3. safi 中placeholder不垂直居中

    用css hack将line-height 设置为1 例子: input{height: 32px; line-height: 32px; [;line-height: 1px;]};

  4. Eclipse 创建新的workspace

    工具:eclipse  版本:4.5.1 1.配置jdk(java-Compiler).maven(Mven-User Settings) 2.从svn拉取项目,创建.project(修改其项目名称) ...

  5. spin_lock、spin_lock_irq、spin_lock_irqsave区别

    void spin_lock(spinlock_t *lock); void spin_lock_irq(spinlock_t *lock); void spin_lock_irqsave(spinl ...

  6. YTU 2457: 很简单的一道题

    2457: 很简单的一道题 时间限制: 1 Sec  内存限制: 128 MB 提交: 261  解决: 80 [提交][状态][讨论版] 题目描述 有一个简单的函数数学公式,如下 输入 重复输入多组 ...

  7. SpringBoot使用logback日志记录

    在resources里的配置文件: logback-spring.xml <?xml version="1.0" encoding="UTF-8" ?&g ...

  8. MySQL性能优化-I/O相关配置参数

    本文介绍InnoDB和MyISAM两种存储引擎的I/O相关参数配置. 1.InnoDB  I/O相关配置 Innodb是一种事务型的存储引擎,为了减少提交事务时产生的io开销,innodb采用了写日志 ...

  9. 一步一步学Silverlight 2系列(12):数据与通信之WebClient

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  10. 简易五子棋 V1.1.0

    main.cpp #include "fivechess.cpp" int main() { fivechess a; a.RunGame(); getchar(); return ...