[leetcode]477. Total Hamming Distance
来源:互联网 发布:人工智能 输入法 编辑:程序博客网 时间:2024/05/02 02:21
题目链接:https://leetcode.com/problems/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
.
class Solution{public: int totalHammingDistance(vector<int>& nums) { int count=0; for(int i=0;i<32;i++) { int bitCount=0; for(int j=0;j<nums.size();j++) { bitCount+=(nums[j]>>i)&1; } count+=bitCount*(nums.size()-bitCount); } return count; }};
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
- 字符串“open_door” 转换成 “OpenDoor”、”make_by_id” 转换成 ”MakeById”。
- String "+"的问题
- php学习笔记-数组篇(4)
- hql语句中要注意的几点
- reactos源码下载地址
- [leetcode]477. Total Hamming Distance
- 公约数和公倍数
- 子线程更新UI的几种方法
- Java 配置文件加载
- maven+Springboot——mapper层的sql执行失败,在service层里不能捕获到
- spring使用JdbcTemplate
- Java对象引用,以及对象赋值
- Nginx反向代理到Tomcat服务器
- 菜鸟程序员如何快速提高技术