LeetCode.477 Total Hamming Distance
来源:互联网 发布:it编程 编辑:程序博客网 时间:2024/06/05 03:32
477. Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Now your job is to find the total Hamming distance between all pairs of the given numbers.
Example:
Input: 4, 14, 2Output: 6Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (justshowing the four bits relevant in this case). So the answer will be:HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.
Note:
- Elements of the given array are in the range of
0
to10^9
- Length of the array will not exceed
10^4
.ot excee
bool compare(int a, int b){return a > b;}int totalHammingDistance(vector<int>& nums){int one = 0, result = 0, sum = nums.size();sort(nums.begin(), nums.end(), compare);while(!nums.empty()){for(int i = nums.size() - 1; i >= 0; i--){if(nums[i] == 0){nums.pop_back();}else if(nums[i] % 2 == 1){nums[i] /= 2;one++;}else{nums[i] /= 2;}}result += (one * (sum - one));one = 0;}return result;}
阅读全文
0 0
- Leetcode Total Hamming Distance 477
- 【LeetCode-477】Total Hamming Distance
- leetcode-477-Total Hamming Distance
- leetcode 477 Total Hamming Distance
- Leetcode 477 Total Hamming Distance
- LeetCode.477 Total Hamming Distance
- LeetCode: Total Hamming Distance
- LeetCode Total Hamming Distance
- leetcode-Total Hamming Distance
- leetcode 477 Total Hamming Distance C++
- LeetCode 477. Total Hamming Distance
- 【Leetcode】477. Total Hamming Distance
- LeetCode 477. Total Hamming Distance
- [leetcode] 477. Total Hamming Distance
- LeetCode 477. Total Hamming Distance
- LeetCode 477. Total Hamming Distance
- LeetCode 477. Total Hamming Distance
- Leetcode-477. Total Hamming Distance
- python学习三个月001
- 浅谈面向对象--《Thinking in Java》读书笔记(一)
- 多线程间通信的方法
- HDU 4035 Maze(概率DP)
- JAVA设计模式之单例模式
- LeetCode.477 Total Hamming Distance
- 二维数组与数组指针
- Redis系列-1.介绍
- url 在chrome可以 firefox可以 IE报错?
- [安全漏洞公告专区]【漏洞公告】WordPress的Display Widgets插件存在后门
- 删除git本地分支遇到的一个小问题
- Robot Framework+Appium案例分享一: 验证版本信息
- 关于MYSQL 的 AUTO-INC Locks
- vcpu和cpu的关系