532. K-diff Pairs in an Array
来源:互联网 发布:网络空间用什么来描绘 编辑:程序博客网 时间:2024/05/16 12:10
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 = 2Output: 2Explanation: 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 = 1Output: 4Explanation: 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 = 0Output: 1Explanation: There is one 0-diff pair in the array, (1, 1).
Note:
The pairs (i, j) and (j, i) count as the same pair.
The length of the array won’t exceed 10,000.
All the integers in the given input belong to the range: [-1e7, 1e7].
Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].
翻译
给定一个整数数组和一个整数k,您需要找到数组中唯一的 k-diff对数。这里,k-diff对被定义为整数对(i,j),其中i和j都是数组中的数字,它们的绝对差是k。
示例1:
输入: [ 3,1,4,1,5 ],k = 2
输出: 2
说明:阵列中有两个2-diff对,(1,3)和(3,5)。
虽然我们在输入中有两个1,但我们应该只返回唯一对的数量。
示例2:
输入: [ 1,2,3,4,5 ],k = 1
输出: 4
说明:阵列中有四个1-diff对,(1,2),(2,3),(3,4)和(4,5)。
示例3:
输入: [ 1,3,1,5,4 ],k = 0
输出: 1
说明:阵列中有一个0-diff对(1,1)。
注意:
对(i,j)和(j,i)计数为同一对。
阵列的长度不会超过10,000。
给定输入中的所有整数属于范围:[-1e7,1e7]。
解题思路
这题做的不太好,做的麻烦了,网上的解法有些还挺好的,但是绝对是满足要求的,利用了map的键值对,其中键存为3 1这种形式,表示映射关系,其中前面的数字表示出现过,后面的数字表示需要该数字
public static int findPairs(int[] nums, int k) { Map<String,Boolean> m = new HashMap<>(); int length=0; int []flag={k,-k}; if(k<0) return 0; else if(k==0){ for(int i=0;i<nums.length;i++){ if(m.get(String.valueOf(nums[i]))==null) m.put(String.valueOf(nums[i]),false); else if(!m.get(String.valueOf(nums[i]))){ m.put(String.valueOf(nums[i]),true); length++; } } }else{ for(int i=0;i<nums.length;i++){ for(int j=0;j<flag.length;j++){ String posKey = String.valueOf(nums[i]) + " " + String.valueOf(nums[i]+flag[j]); String negKey = String.valueOf(nums[i]+flag[j]) + " " + String.valueOf(nums[i]); if(m.get(posKey) == null && m.get(negKey) == null){ //System.out.println(posKey); //System.out.println(negKey); m.put(posKey, false); //m.put(negKey, false); } else if(m.get(negKey) != null){ if(!m.get(negKey)){ //System.out.println(posKey); //System.out.println(negKey); m.put(posKey, true); m.put(negKey, true); length++; } } } } } return length;}
欢迎加入中科院开源软件自习室:631696396
- 532. K-diff Pairs in an Array
- 532. K-diff Pairs in an Array
- 532. K-diff Pairs in an Array
- 532. K-diff Pairs in an Array
- 532. K-diff Pairs in an Array
- 532. K-diff Pairs in an Array
- 532. K-diff Pairs in an Array
- 532. K-diff Pairs in an Array
- 532. K-diff Pairs in an Array
- 532. K-diff Pairs in an Array
- 532. K-diff Pairs in an Array
- 532. K-diff Pairs in an Array
- 532. K-diff Pairs in an Array
- 532. K-diff Pairs in an Array
- 532. K-diff Pairs in an Array
- 532. K-diff Pairs in an Array
- K-diff Pairs in an Array
- leetcode532~K-diff Pairs in an Array
- spring boot 集成druid,监控配置
- 套接字编程(一)----基于TCP协议
- 二级域名配置(万网 + tomcat8.5) 解析到子目录
- python基础知识
- Python爬虫简单实战:抓取小猪短租西安市前五页民房数据
- 532. K-diff Pairs in an Array
- WPF NotifyIcon ContextMenu 右键菜单 样式Style
- 大学计算机模拟考试常见试题与解析(二)
- 语言,框架等等工具的选择
- 乘积最大
- 2017的OI之路的梦想
- Conway's law
- netty4 UDP的使用
- 二叉搜索树