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;
}
};

最新文章

  1. ionic
  2. java图形界面写个小桌面,内置简单小软件
  3. 移动端调试工具DebugGap推荐。
  4. Day Four(Beta)
  5. P1311 选择客栈
  6. context:annotation-config 与context:component-scan
  7. java获取泛型的真实类型
  8. Hadoop on Mac with IntelliJ IDEA - 10 陆喜恒. Hadoop实战(第2版)6.4.1(Shuffle和排序)Map端 内容整理
  9. Oracle语句块PL/SQL循环判断
  10. VS2010 配置 DirectX 开发环境
  11. The Building Blocks-Components of EA part 1- Information and Strategy
  12. building system busy, pls wait !!
  13. JS中的作用域以及全局变量的问题
  14. ubuntu上的附件-终端和用快捷键ctrl+alt+f1 有啥区别
  15. 利用requirejs实现vue的模块化开发
  16. Django signals 信号作用及用法说明
  17. Hadoop Shell命令(基于linux操作系统上传下载文件到hdfs文件系统基本命令学习)
  18. python之模块datetime详解
  19. Git提交代码报错Git push error:src refspec XXX matches more than one解决方案
  20. Oracle体系结构之控制文件的多路复用技术

热门文章

  1. SP Flash Tool使用异常集锦
  2. 【LeetCode】Roman to Integer &amp; Integer to Roman
  3. 判断安卓和IOS
  4. Python 基礎 - 字符串常用操作
  5. XmlSerializer(Type type, Type[] extraTypes) 内存泄漏
  6. 网站统计中的数据收集原理及实现(share)
  7. Android Studio的简单设置:
  8. 神奇的sort()函数
  9. C#中的选择语句
  10. pwnable echo2