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. Back to openGL!

    #include <iostream> #include <windows.h> #include <gl/glut.h> #include <math.h& ...

  2. iOS UITextField限制输入数字

    有时候项目中要求文本框中只能输入数字,如:价格.公里数.费用等等,一般的文本框不限制输入的格式,这时候只能强制限制输入框的输入格式了,代码如下: #import "ViewControlle ...

  3. PHP中"->"和"=>"的区别

    =>不是运算符,这个是数组特有的指针符号..是定义数组时做键.值映射用的.foreach($_POST AS $key=>$value){...}遍历数组的时候,就是将$_POST数组的键 ...

  4. Maven项目pom.xml文件详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  5. "****" is not translated in zh, zh_CN.的解决方法

    最近在开发一个app,要用到静默安装等一些小技术,但是引发了问题如下: 在Android SDK Tool r19之后, Export的时候遇到xxx is not translated in yyy ...

  6. LayaAir引擎——(十)

    var k = new Array(); var l = new Array(); var m = new Array(); var zhiyeCurosr = 0; function zyinit( ...

  7. Swift中文教程 第2章 基本运算符 (20140604更新)

    今天我在下午又进行了新的翻译,之前我翻译的大家有没有看啊,感觉如何,我昨天下午不小心点了什么原先的文章都没了,大家的评论也没了,现在我也找不回来了,希望大家能继续给我新的评论和支持点个推荐,毕竟现在学 ...

  8. spring HandlerInterceptorAdapter拦截ajax异步请求,报错ERR_INCOMPLETE_CHUNKED_ENCODING

    话不多说,直接上正文. 异常信息: Failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING 问题描述: 该异常是在页面发送ajax请 ...

  9. python 抓取百度音乐

    # coding:utf-8 import urllib2 import re import urllib import chardet from json import * category = ' ...

  10. div各种距离 详细解释图

    详细博文介绍:http://blog.csdn.net/fswan/article/details/17238933