算法(1)K-diff Pairs in an Array
写在前面:研究操作系统,习惯了用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的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- [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 ...
- 51nod 1290 Counting Diff Pairs | 莫队 树状数组
51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...
- C#LeetCode刷题之#532-数组中的K-diff数对(K-diff Pairs in an Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3716 访问. 给定一个整数数组和一个整数 k, 你需要在数组里找 ...
- [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 ...
随机推荐
- 20181009noip HZ EZ两校联考sum(莫队,组合数学)
题面戳这里 思路: noip考莫队???!!! 考场上死活没往这方面想啊!!!数据分治忘写endl50pts滚粗了 这里每个询问都有n,m两个参数 我们可以把它看做常规莫队中的l和r 然后利用组合数的 ...
- 在cmd下面执行.py文件时提示ModuleNotFoundError 但是 IDE 不报错
原理是 python 解释器寻找 模块的顺序决定,不细说 简略来讲就是 在 IDE中运行,会自动帮你把项目根目录添加到 PYTHONPATH 中,但是在 cmd 运行需要自己添加. 解决方法: 1. ...
- ABAP术语-Customer Exit
Customer Exit 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/19/1045231.html Pre-planned enhan ...
- 转:Zookeeper客户端Curator使用详解
原文:https://www.jianshu.com/p/70151fc0ef5d Zookeeper客户端Curator使用详解 前提 最近刚好用到了zookeeper,做了一个基于SpringBo ...
- Cygwin安装篇,windows平台上运行的类UNIX模拟环境
1.虚拟光驱的安装 虚拟光驱下载 一路下一步,不再阐述,这些广告选项不要选 2.安装文档,双击ISO文档 ISO下载地址 链接:http://pan.baidu.com/s/1miFVCYO 密码:z ...
- 【解决】MongoDB 线上业务处理,数据去重脚本实现
mongo客户端工具下载 https://robomongo.org/download 线上业务,k线 展示出现问题,相同时间戳的数据多次插入导致数据不真实,后经排查发现是每次都是写的四条数据, ...
- Co. - Microsoft - Windows - 通过任务计划,备份本地MySQL,数据上传Linux备份服务器
需求 客户为Windows系统,安装MySQL,需要每日备份数据库到指定目录,并且上传到公司的备份服务器(Linux). 1.使用mysqldump备份MySQL数据库,使用FTP上传到阿里云Linu ...
- C# 生成机器码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- [Cracking the Coding Interview] 4.4 Check Balanced
Implement a function to check if a binary tree is balanced. For the purpose of this question, a bala ...
- kafka重置offset
kafka重置offset 1.删除zookeeper上节点信息 打开client :./zkCli.sh -server 127.0.0.1:12181 删除consumer:rmr /cons ...