传送门

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. 配置sde使可以使用sde sql(ST_Geometry)操作空间数据

    用处:进行此配置后,可以用sql语言,与sde空间数据库进行空间查询,增删改图层的要素等 PS:同时也是解决 ORA-28595Extproc 代理 DLL 路径无效 的方法 ORA-06512: 在 ...

  2. ZPL文件打印

    ZPL:全称是Zebra Printer Language,目前能够直接打印zpl文件的打印机只有斑马打印机 如何打印zpl文件呢? 软件类: 一.Print Conductor(桌面打印软件) ht ...

  3. ListView鼠标拖

    private Point Position = new Point(0, 0); private void treeFileView_ItemDrag(object sender, ItemDrag ...

  4. ES6——Promise

    异步和同步 异步,操作之间没有关系,同时执行多个操作, 代码复杂 同步,同时只能做一件事,代码简单 Promise 对象 用同步的方式来书写异步代码 Promise 让异步操作写起来,像在写同步操作的 ...

  5. vue.js(11)--案例--关键字搜索列表

    关键字搜索品牌案例 (1)页面布局 <div class="app"> <div class="panel panel-primary"> ...

  6. 有用的2个 Windows 下批处理文件(bat文件):

    创建多个文件夹(目录): 新建一个记事本:win + R –>输入notepad 在笔记本中输入如下: mkdir Would mkdir you mkdir marry mkdir me 另存 ...

  7. JS删除对象中的某一属性(delete)

    var obj= {} 1.JS对象添加新属性 obj.address="shenzhen" 2.JS删除对象中的某一属性(delete) var obj= { height: , ...

  8. [APIO2009]抢掠计划(Tarjan,SPFA)

    [APIO2009]抢掠计划 题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是, ...

  9. django之创建项目

    1.创建虚拟环境 mkvirtualenv django_study -p python3 创建成功后:(django_study) python@ubuntu:~$ 2.安装django-指定版本1 ...

  10. CollectionUtils工具类中常用方法

    @SuppressWarnings("rawtypes") @Test public void test1() { List<String> coll = new Ar ...