写在前面:研究操作系统,习惯了用C,但是在做算法题甚至构建大型系统时,C真的是噩梦。还是用C++比较好,基本算法很成熟,并可基于此实现更复杂的算法。那就边写算法边捡起来好久不用的C++吧!

题目:数组中的k差对(K-diff Pairs)。输入为一个数组A和一个整数k,找到数组中 所有的数值对pairs(i,j),其中A[i]和A[j]之间差的绝对值是k。比如【3,1,4,1,5】,k=2,ouput=2,这样的差值对是(1,3)和(3,5),其中1虽然在数组中出现过2次,但是只算一次。(题目:https://leetcode.com/problems/k-diff-pairs-in-an-array/#/description)

思路:把数组中的元素放到map中,key是数组中的值,value是该值在数组中出现的次数。构建好map之后,遍历数组,看看a[i]+k这个值在数组中是否存在。

答案:https://github.com/honpey/codebox/blob/master/leetcode/array/p532.cpp

为什么要用unordered_map?因为后面涉及到查找,unordered_map查找是不是更慢一些,但是插入时就简单一些;这个题目中数据量不大,所以直接就用unordered_map了。unordered_map使用hash表实现,在只要访问不要排序的场景适用,搜索时间复杂度是O(1);但是map使用RB-tree实现,搜索、插入、删除的时间复杂度都是O(logN),但是这并不是表示unordered_map就一定是好的,还是要看数据量的。unordered_map和hash_map干了一样的事儿,但是hash_map仍没写入C++11里。

相关知识点

C++中数据结构:map,vector,auto变量

当使用到C++11中的结构体时,比如unordered_map时,编译时加上:g++ -std=c++11 -o $@ $<

之前错误思路

算法(1)K-diff Pairs in an Array的更多相关文章

  1. 532. K-diff Pairs in an Array绝对值差为k的数组对

    [抄题]: Given an array of integers and an integer k, you need to find the number of unique k-diff pair ...

  2. LeetCode 532. K-diff Pairs in an Array (在数组中相差k的配对)

    Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...

  3. [LeetCode] K Inverse Pairs Array K个翻转对数组

    Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...

  4. [LeetCode] K-diff Pairs in an Array 数组中差为K的数对

    Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...

  5. [Swift]LeetCode629. K个逆序对数组 | K Inverse Pairs Array

    Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...

  6. 629. K Inverse Pairs Array

    Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...

  7. [Swift]LeetCode532. 数组中的K-diff数对 | K-diff Pairs in an Array

    Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...

  8. 51nod 1290 Counting Diff Pairs | 莫队 树状数组

    51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...

  9. C#LeetCode刷题之#532-数组中的K-diff数对(K-diff Pairs in an Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3716 访问. 给定一个整数数组和一个整数 k, 你需要在数组里找 ...

  10. [leetcode-532-K-diff Pairs in an Array]

    Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...

随机推荐

  1. 『嗨威说』常见的C++函数模板整理(一)

    开学两天,身上的职责直接变为两个班班长,三个小组组长,哇这事情估计够我忙活了,想躲都躲不掉啊,看来我还是真招人推荐各种管理职务啊,以后要是有人推荐我当经理啊领导啊该多好哈哈哈哈.记得今天奶奶生日,很开 ...

  2. Centos防火墙的配置

    Selinux的三种模式:enforcing,passive,disable 临时更改模式:setengorce 1|0        1:enforcing,   0:passive [root@C ...

  3. (转)Dubbo 简单Dome搭建

    (转)原地址https://blog.csdn.net/noaman_wgs/article/details/70214612/ Dubbo背景和简介 Dubbo开始于电商系统,因此在这里先从电商系统 ...

  4. dedesmc 手机端生成静态页

    dedesmc 手机端生成静态页 1.首先下载插件,下载地址:https://pan.baidu.com/s/1Nfx_KBYuxRkZ7VzoPxy28g 密码:83x7 2.进入 dedecms ...

  5. Java : java基础(3) IO流

    流按操作类型分为两种,字节流,字符流,按流向分为输入流,输出流,输入流的抽象父类InputStream,输出流抽象父类OutputStream,字符流的抽象父类是Reader和Writer 一般用字节 ...

  6. Yaf学习(二)----Yaf初体验

    1.hello world 1.1 用yaf输出hello world 1.首先配置host,nginx 2.host不用多说,指向虚拟机IP即可 1.2 重点说一下nginx (只说server块) ...

  7. 《python编程从入门到实践》第六章笔记

    1.字典 字典:一系列键-值对,每一个键都与每一个值相关联.与键相关联的值可以是数字.字符串.列表和字典. 最简单的字典只有一个键值对. eg: alien = {'color':'green','p ...

  8. Python学习:If 语句与 While 语句

    If 语句 用以检查条件:如果条件为真(True),将运行这一块的语句(称作 if-block 或 if 块)    则将运行另一块语句(称作 else-block 或 else 块),其中 else ...

  9. 07 json与os模块(进阶)

    json和os模块 阶段一 .数据交换 1.json的基本介绍 JSON全名是JavaScript Object Notation(即:JavaScript对象标记)它是JavaScript的子集. ...

  10. Unicode控制字符

    Unicode控制字符就是特殊的Unicode字符 控制字符转义代码对照表 Unicode-控制字符 ‎LRM ‏RLM ‍ZWJ ‌ZWNJ ‪LRE ‭LRO ‮RLO ‬PDF NADS  ...