写在前面:研究操作系统,习惯了用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. Percona-Tookit工具包之pt-show-grants

      Preface       User privileges regulation is pretty important in DBAs routine job.As we all know,it ...

  2. (转)Dubbo 简单Dome搭建

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

  3. tp3.2报错;syntax error, unexpected 'function' (T_FUNCTION), expecting identifier (T_STRING) or \\ (T_NS_SEPARATOR)

    出错原因:这个是php版本问题,laravel5.1的php版本要求是PHP >= 5.5.9,切换一下PHP版本就行.

  4. Could not obtain transaction-synchronized Session for current thread 错误的解决方法!

    BsTable bsTable = new BsTable(); // String time = request.getParameter("date"); String tim ...

  5. 2.3 进程控制之exec函数族

    学习目标:学习使用exec函数族的重要的几个函数  一.引言 进程通过exec函数根据指定的文件名或目录名执行另一个可执行文件,当进程调用exec函数时,该进程的数据段.代码段和堆栈段完全被新程序替换 ...

  6. DSP+ARM多核异构开发环境SYSLINK搭建OMAPL138

    DSP+ARM多核异构开发环境搭建OMAPL138 注意: 环境为Ubuntu 12.04 只能是这个环境.我甚至在Ubuntu16.04上面安装了VMware,然后,在装了一个Ubuntu 12.0 ...

  7. 数据库中pymysql模块的使用

    pymysql 模块 使用步骤: 核心类Connect链接用和Cursor读写用 1. 与数据库服务器建立链接 2. 获取游标对象(用于发送和接收数据) 3. 用游标执行sql语句 4. 使用fetc ...

  8. AES128加密-S盒和逆S盒构造推导及代码实现

    文档引用了<密码编码学与网络安全--原理和实践>里边的推导过程,如有不妥,请与我联系修改. 文档<FIPS 197>高级加密标准AES,里边有个S盒构造,涉及到了数论和有限域的 ...

  9. java 获取图片大小(尺寸)

    1,获取本地图片大小(尺寸) File picture=new File(strSrc);BufferedImage sourceImg=ImageIO.read(new FileInputStrea ...

  10. ionic 打包apk Failure [INSTALL_FAILED_USER_RESTRICTED: Install canceled by user]

    错误日志如下: Built the following apk(s): /Users/hongye0/Documents/project/haitoujiaApp/platforms/android/ ...