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 the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.
Example 1:
Input: [3, 1, 4, 1, 5], k = 2
Output: 2
Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of unique pairs.
Example 2:
Input:[1, 2, 3, 4, 5], k = 1
Output: 4
Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
Example 3:
Input: [1, 3, 1, 5, 4], k = 0
Output: 1
Explanation: There is one 0-diff pair in the array, (1, 1).
思路:
求出nums的长度,如果k<0或者nums的长度小于等于1,直接返回0。将nums排序,定义用来存储没出现过的元素和用来存储重复的元素的无序set: set1,sameset。开始循环,如果k不等于0,当set1中没有nums[i]但有nums[i]-k ,++result,不管什么情况,并将nums[i]放到set1中去; 如果k等于0,当sameset中没有nums[i]但set1中有,++result并将nums[i]放到sameset中,不管什么情况,都将nums[i]放到set1中。
代码:
class Solution {
public:int findPairs(vector<int>& nums, int k) {int len = nums.size();//求出nums的长度if (k<0 || len <= 1){//如果k<0或者nums的长度小于等于1,直接返回0return 0;}int result = 0; sort(nums.begin(),nums.end());//将nums排序 unordered_set<int> set1;//用来存储没出现过的元素 unordered_set<int> sameset;//用来存储重复的元素 for (int i = 0; i < len; i++) { if(k!=0){//如果k不等于0if (!set1.count(nums[i]) && set1.count(nums[i] - k))//如果set1中没有nums[i]但有nums[i]-k result++;//计数set1.insert(nums[i]);//不管什么情况,并将nums[i]放到set1中去 } else{ //如果if (!sameset.count(nums[i]) && set1.count(nums[i])) {//如果sameset中没有nums[i]但set1中有result++; //计数sameset.insert(nums[i]); //并将nums[i]放到sameset中} set1.insert(nums[i]);//不管什么情况,都将nums[i]放到set1中}} return result; }
};
输出结果:36ms
注:下面是两个我优化之前的代码,复杂度感人
代码2:
class Solution {
public:int findPairs(vector<int>& nums, int k) {vector<int> tmp;int count=0;if(k<0||nums.size()<=1){return 0;}for(int i=0;i<nums.size()-1;++i){for(int j=i+1;j<nums.size();++j){if(k==nums[i]-nums[j]){++count;tmp.push_back(nums[i]);for(int k=0;k<tmp.size()-1;++k){if(tmp[k]==tmp[tmp.size()-1]){tmp.pop_back();--count;}}}else if(-k==nums[i]-nums[j]){++count;tmp.push_back(nums[j]);for(int k=0;k<tmp.size()-1;++k){if(tmp[k]==tmp[tmp.size()-1]){tmp.pop_back();--count;}} }}}return count;}
};
**输出结果:**706ms
代码3:
class Solution {
public:int findPairs(vector<int>& nums, int k) {unordered_map<int,int> tmp;int count = 0;if (k<0 || nums.size() <= 1){return 0;}for (size_t i = 0; i<nums.size() - 1; ++i){for (size_t j = i + 1; j < nums.size(); ++j){if (k == nums[i] - nums[j] && !tmp.count(nums[i])){tmp[nums[i]] = nums[j];++count;}else if (-k == nums[i] - nums[j] && !tmp.count(nums[j])){tmp[nums[j]] = nums[i];++count;}}}return count;}
};
输出结果:359 ms
LeetCode 532. K-diff Pairs in an Array相关推荐
- LeetCode算法题-K-diff Pairs in an Array(Java实现)
这是悦乐书的第254次更新,第267篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第121题(顺位题号是532).给定一个整数数组和一个整数k,您需要找到数组中唯一的k- ...
- C#LeetCode刷题之#532-数组中的K-diff数对(K-diff Pairs in an Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3716 访问. 给定一个整数数组和一个整数 k, 你需要在数组里找 ...
- 剑指offer 最小的k个数 leetcode 215. Kth Largest Element in an Array
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- leetcode【845】Longest Mountain in Array
写在最前面:不得不说,python对于数据结构的处理非常自然,几乎就是你怎么想的直接写出来就可以,不用太过在意转化,代价就是,性能较差 leetcode[845]Longest Mounta in A ...
- LeetCode算法入门- Remove Duplicates from Sorted Array -day21
LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...
- LeetCode 852. Peak Index in a Mountain Array
LeetCode 852. Peak Index in a Mountain Array Solution1:我的答案 时间复杂度O(n)O(n)O(n) class Solution { publi ...
- LeetCode 787. K 站中转内最便宜的航班(图/Bellman Ford算法)
文章目录 贝尔曼-福特算法(Bellman-Ford) 简介 算法思想 算法执行过程 应用 题目描述 分析 代码 LeetCode 787. K 站中转内最便宜的航班 题目描述 Bellman For ...
- LeetCode——787. K 站中转内最便宜的航班(Cheapest Flights Within K Stops)[中等]——分析及代码(Java)
LeetCode--787. K 站中转内最便宜的航班[Cheapest Flights Within K Stops][中等]--分析及代码[Java] 一.题目 二.分析及代码 1. 动态规划 ( ...
- LeetCode 787.K站中转内最便宜的航班
LeetCode 787.K站中转内最便宜的航班 有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 ds ...
最新文章
- [JSOI2018]军训列队
- IIS6的工作进程回收
- linux中sort不对的问题
- ibatis迁移到mybatis3
- Eval.cs的改进版 动态编译类,可以计算表达式,也可以调用系统中的类
- jsp mysql demo_利用JSP+MYSQL实现注册+登入的demo----0001
- 浅析ios开发中Block块语法的妙用
- postgresql目录
- springboot整合postgre和hbase实现互相交互功能
- 使用外星人进行测试:如何使用Arquillian测试JPA类型转换器
- java中contains的用法_java容器中所有接口和类的用法
- Wisconsin Squares [USACO]
- JAVA入门到精通-第7讲-成员方法-构造方法
- IIS 7.5 下PHP(FastCGI模式)配置手记
- 阿里 java ide_纯JAVA版JAVA IDE环境(源码)
- 学生用计算机重启,如何使学校的电脑在重启后不自动还原系统?
- 解决MDK(Keil)Download图标灰色不能下载的问题
- 中级软件设计师知识点总结
- .nav ul .mall a:hover是什么意思
- 《Adaptive Unimodal Cost Volume Filtering for Deep Stereo Matching》
热门文章
- 布袋除尘器过滤风速多少_滤筒除尘器的过滤风速如何选择?不会的进来看
- app做好后如何上线_传统企业如何做好线上线下全网营销?不知道的建议看完这篇干货...
- MySQL:备份数据库脚本报错mysqldump: Couldn‘t execute ‘SELECT COLUMN_NAME****
- STM32那点事(3)_中断(下)
- 三通道的黑白图(不同于单通道的普通的黑白图片)
- tf.keras.layers.Permute
- vue 页面不置顶问题(页面内操作、页面跳转后) - 集合篇
- 微博超话签到小工具v2.0 签到管理操作
- Discuz模板 轻社区Qing_freefresh 完整版
- linux自动重启保护,Linux在崩溃时自动重启应用程序 – 守护进程