当前位置:网站首页>力扣解法汇总532-数组中的 k-diff 数对
力扣解法汇总532-数组中的 k-diff 数对
2022-07-22 08:55:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
给你一个整数数组 nums 和一个整数 k,请你在数组中找出 不同的 k-diff 数对,并返回不同的 k-diff 数对 的数目。
k-diff 数对定义为一个整数对 (nums[i], nums[j]) ,并满足下述全部条件:
0 <= i, j < nums.length
i != j
nums[i] - nums[j] == k
注意,|val| 表示 val 的绝对值。
示例 1:
输入:nums = [3, 1, 4, 1, 5], k = 2
输出:2
解释:数组中有两个 2-diff 数对, (1, 3) 和 (3, 5)。
尽管数组中有两个 1 ,但我们只应返回不同的数对的数量。
示例 2:
输入:nums = [1, 2, 3, 4, 5], k = 1
输出:4
解释:数组中有四个 1-diff 数对, (1, 2), (2, 3), (3, 4) 和 (4, 5) 。
示例 3:
输入:nums = [1, 3, 1, 5, 4], k = 0
输出:1
解释:数组中只有一个 0-diff 数对,(1, 1) 。
提示:
1 <= nums.length <= 104
-107 <= nums[i] <= 107
0 <= k <= 107
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/k-diff-pairs-in-an-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 使用动态区间的概念,首先对数组进行排序。 * 然后规划一个区间[left,right] * 如果right-left>k,那么left向右移动找到不等于当前值的数。 * 如果right-left<k,那么right向右移动找到不等于当前值的数。 * 如果right-left=k,那么left向右移动,记录num++。 * 如果key==0,则记录重复的数的个数即可。
代码:
public class Solution532 {
public int findPairs(int[] nums, int k) {
Arrays.sort(nums);
//如果k==0,则计算重复的即可
int num = 0;
if (k == 0) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
}
for (int key : map.keySet()) {
if (map.get(key) > 1) {
num++;
}
}
return num;
}
int left = 0;
int right = 1;
while (left >= 0 && right >= 0) {
int leftValue = nums[left];
int rightValue = nums[right];
int diff = rightValue - leftValue;
if (diff < k) {
right = findNext(nums, right);
} else if (diff > k) {
left = findNext(nums, left);
} else {
num++;
left = findNext(nums, left);
}
}
return num;
}
private int findNext(int[] nums, int index) {
int value = nums[index];
int nextValue = nums[index];
while (value == nextValue) {
if (++index == nums.length) {
return -1;
}
nextValue = nums[index];
}
return index;
}
}
边栏推荐
- 请教下,oracle-cdc是不是不支持检查点,当实时采集过程中任务挂了到重启这段时间的数据变化是不
- 2021-10-18 burn bare board program with EOP
- Reentrant function
- 文件描述符的复制
- 3.Transbot修改显示分辨率
- PostgreSQL 中 analyze table后most_common_elems 字段为什么不填充?
- QT笔记——自定义数据类型
- Gbase8s database minus operator
- Ssl/tls protocol attack detection method based on stream spectrum theory
- Design of miner type identification mechanism based on reputation management model
猜你喜欢
Lesson 12 MySQL high availability component MHA
[10:00 public class]: cloud video conference system privatization practice
「武汉理工大学 软件工程复习」第七章 | 软件测试
"Review of software engineering in Wuhan University of technology" Chapter 5 | software architecture
女嘉賓報名
Fabric.js 居中元素
QT笔记——操作Execl
腾讯持久化框架MMKV原理探究
Dr. water
QT notes - qudpsocket of network communication
随机推荐
力扣解法汇总648-单词替换
Gbase8s database set database object mode statement
QT笔记——QTableWidget表格生成树,QTreeWidget树节点生成表格内容
力扣解法汇总731-我的日程安排表 II
QT笔记——网络通信 之 QUdpSocket
Top 10 active noise reduction headphones and top 10 active noise reduction headphones brands
QT notes - qjason
DistSQL 深度解析:打造动态化的分布式数据库
Gbase8s database minus operator
「武汉理工大学 软件工程复习」第二章 | 软件过程模型
Gbase8s database restrictions on set collection
Summary of front-line Internet P7 interview questions - Supplementary
「武汉理工大学 软件工程复习」第六章 | 编码规范
"Review of software engineering in Wuhan University of technology" Chapter 6 | coding specification
Gbase8s database comparison performed by database objects
【How To 系列】好友裂变平台搭建
Reversible information hiding method of image in ciphertext domain based on fine-grained embedding space reservation
Dr. water
Simplify the complexity and talk about the abstraction of replication state machine system architecture
Rocky基础练习题-shell脚本-1