LeetCode 477. Total Hamming Distance
来源:互联网 发布:淘宝优惠券网站 编辑:程序博客网 时间:2024/05/02 00:52
题目:
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
.
class Solution {public: int totalHammingDistance(vector<int>& nums) { int sum = 0; unsigned int t = 0x1; for(int i = 0; i < 30; i++){ int num1 = 0, num0 = 0; for(int j = 0; j < nums.size(); j++) { int tmp = t&nums[j]; if(tmp>0) { num1++; } else { num0++; } } sum+=num1*num0; t = t<<1; } return sum; }};
阅读全文
0 0
- 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
- 【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
- LeetCode 477. Total Hamming Distance
- leetcode 477. Total Hamming Distance
- 发现一个非常好用的扒站工具IDM
- cocos2dx-3.x HTTP 请求调用
- JDBC操作数据库之查询数据
- Java设计模式之《代理模式》及应用场景
- 在BW系统中,如何查询某一个权限对象在哪些角色中出现过
- LeetCode 477. Total Hamming Distance
- python -字典
- java命名规范
- 个人模拟面试之基础知识+部分线程(从其他面经总结而来)
- 初学C++编程常见错误
- 360美猴王2,米家,视连科OS3同步实测对比(更新)
- Python3 PyQt5 PyCharm 环境搭建
- 简单图片瀑布流
- canvas3d学习一 应用结构