传送门

Description

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, 2

Output: 6

Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just
showing 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:

  1. Elements of the given array are in the range of 0 to 10^9
  2. Length of the array will not exceed 10^4.

思路

题意:给定一个数组,其中两两配对,求出所有配对数的汉明距离。

题解:我们知道,汉明距离是两个数其二进制位上不同的位数,因此,对于32位数,遍历二进制位,统计每个数在第一个bit位有多少个数是1,有多少个数是0,然后统计第二个bit位,以此类推。

class Solution {
public:
//59ms
int totalHammingDistance(vector<int>& nums) {
int res = 0,len = nums.size();
for (int i = 0;i < 32;i++){
int cnt = 0;
for (int j = 0;j < len;j++){
cnt += (nums[j] >> j) & 1;
}
res += (len - cnt) * cnt;
}
return res;
}
};

  

[LeetCode] 477. Total Hamming Distance(位操作)的更多相关文章

  1. [LeetCode] 477. Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  2. LeetCode "477. Total Hamming Distance"

    Fun one.. the punch line of this problem is quite common in Bit related problems on HackerRank - vis ...

  3. 【LeetCode】477. Total Hamming Distance 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...

  4. 477. Total Hamming Distance总的二进制距离

    [抄题]: The Hamming distance between two integers is the number of positions at which the correspondin ...

  5. 461. Hamming Distance and 477. Total Hamming Distance in Python

    题目: The Hamming distance between two integers is the number of positions at which the corresponding ...

  6. 461. Hamming Distance + 477. Total Hamming Distance

    ▶ 与 Hamming  距离相关的两道题. ▶ 461. 求两个数 x 与 y 的哈夫曼距离. ● 代码,4 ms,对 x 和 y 使用异或,然后求值为 1 的位的个数. class Solutio ...

  7. 477 Total Hamming Distance 汉明距离总和

    两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量.计算一个数组中,任意两个数之间汉明距离的总和.示例:输入: 4, 14, 2输出: 6解释: 在二进制表示中,4表示为0100,14表 ...

  8. 477. Total Hamming Distance

    class Solution { public: int totalHammingDistance(vector<int>& nums) { ; ; i < ; i++) { ...

  9. [LeetCode] Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

随机推荐

  1. 生产者消费者模型(JoinableQueue)

  2. win32 socket 编程(一)——TCP/IP

    一.基本概念 a) 同步:指发送方发出数据后,等收到接收方发回的响应,才发下一个数据包的通信方式. nb)异步:指的是发送方不等接收方响应,便接着发下个数据包的通信方式. c) 阻塞:指调用某函数时, ...

  3. JSP学习(2)

    JSP学习(2) JSP简介 Java Server Page,其根本是一个简单Servlet设计. 常用的动态网站开发技术 JSP:安全性高,适合开发大型的,企业级或分布式的Web应用程序. Asp ...

  4. 了解Greenplum (2)

    一.目的 1. 理解Greenplum中的数据分布策略(random 和 distribution),分析不同分布策略的优劣:2. 理解查询执行中的数据广播和数据重分布,分析在何种情况下选择哪种策略, ...

  5. python基础--2 字符串

    整型 int python3里,不管数字多大都是int类型 python2里面有长整型long 将整型字符串转换为数字 # a='123' # print(type(a),a) # b=int(a) ...

  6. idea中ehcahe配置中 Cannot find the declaration of element 'ehcache'.

    ehcahe.xml 中报错: Cannot find the declaration of element 'ehcache'. 打开settings->languages&frame ...

  7. python 日期封装

    import time import datetime import locale class TimeUtil: def __init__(self, curtime=None): self.cur ...

  8. UI定位元素大全(跟App定位元素差不多哦)

    selenium+python自动化之元素定位 作者:一飞冲天 同样的道理,把一个页面上的元素当成是一个对象(你的女神),我们就可以通过她的属性值来找到她,比如她性别女爱好爬山---------你就可 ...

  9. Intent.java分析

    代码位于frameworks/base/core/java/anroid/Content/Intent.java Intent是对要进行操作的一种抽象描述.用action抽象操作,用data(andr ...

  10. linux运维、架构之路-Nginx服务

    一.Nginx服务 1.介绍         Nginx软件常见的使用方式或架构为:LNMP(linux nginx mysql php),Nginx三大主要功能,web网站服务,反向代理负载均衡(n ...