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