Fun one.. the punch line of this problem is quite common in Bit related problems on HackerRank - visualize it in your mind, and you will find: all bits on the same index among all numbers, will not involve other bits on other indices. So, we simply count number of 0s or 1s, on the same bit index from all numbers - we count it vertically..

class Solution {
public:
int totalHammingDistance(vector<int>& nums)
{
int n = nums.size();
if(n < ) return ; // Pass 1: count num of 1s O(31n) -> O(n)
vector<unsigned> cnt();
for(auto v : nums)
for(int i = ; i < ; i ++)
cnt[i] += (v & ( << i)) ? : ; // Pass 2: count
int ttl = ;
for(int i = ; i < ; i ++)
ttl += cnt[i] * (n - cnt[i]); return ttl;
}
};

LeetCode "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. 477 Total Hamming Distance 汉明距离总和

    两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量.计算一个数组中,任意两个数之间汉明距离的总和.示例:输入: 4, 14, 2输出: 6解释: 在二进制表示中,4表示为0100,14表 ...

  8. 477. Total Hamming Distance

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

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

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

随机推荐

  1. Uber优步宁波司机注册正式开始啦! UBER宁波司机注册指南!

      自2012年Uber开始向全球进军以来,目前已进入全球56个国家和地区的市场,在全球超过270个城市提供服务, 而Uber公司的估值已高达412亿美元. [目前开通Uber优步叫车服务的中国城市] ...

  2. MISC-极客大挑战-pen_and_apple.rar

    额   一道MISC的题目 关键词:NTFS数据流隐藏 链接:http://pan.baidu.com/s/1c14PM3A 密码:d7hn 拿到的是一个rar压缩文件,解压得到一个无节操的最近很流行 ...

  3. 项目中 poi 导出 出现html特殊符号的实体 (已解决)

    导出excel 时出现 类似这样的>  符号 , 大概是存到数据库也是这样,然后jsp解析可以解析出来,但是java不认得,需要个人写出解析方法. 废话不说,贴码: /** *转换html特殊符 ...

  4. github提交代码流程:

    (1)  检查一遍代码改动          $git status (2) 将工作目录中的代码提交到暂存区 $ git add filename     git add -A (3)  提交代码到本 ...

  5. peersim中BT网络核心代码解析

    首先大概介绍BT网络运行的整体流程: 开始阶段,一个节点加入到网络中,并向tracker节点发送信息,tracker返回若干个邻居的列表 得到列表后,向每个邻居发送bitfiled信息,来获取他们的文 ...

  6. WeView 里引用的H5中的文字 到行末尾 文字被切割

    这个情况 在iPhone6以上没问题  以下有问题  具体情况是 我用以下代码计算内容的高度 NSString *injectionJSString = @"var script = doc ...

  7. C#的IPAddress IPEndPoint

    以前觉得什么都能记住 翻一遍书就能去考试了,现在回过头来想一些东西,突然有种模糊的陌生感,应了那句好记性不如烂笔头.做笔记终归是利大于弊的.麻烦一点但是却受用. 突然想从头开始,看一些过去的书,补一些 ...

  8. Caffe.proto使用

    参考 http://blog.csdn.net/qq_16055159/article/details/45115359 书写.proto文件 作用:编写一个 proto 文件,定义我们程序中需要处理 ...

  9. apache安全配置---禁止访问特定文件,防止日志、压缩包被下载

    指定禁止访问 某些后缀的文件 修改apache配置httpd.conf,在最后加上配置后,重启apache <Files ~ ".txt|.log|.zip|.gz|.sql" ...

  10. imshow() displays a white image for a grey image

    Matlab expects images of type double to be in the 0..1 range and images that are uint8 in the 0..255 ...