File tree 5 files changed +96
-0
lines changed
algorithms/KDiffPairsInAnArray
5 files changed +96
-0
lines changed Original file line number Diff line number Diff line change @@ -187,6 +187,7 @@ All solutions will be accepted!
187
187
| 7| [ Reverse Integer] ( https://leetcode-cn.com/problems/reverse-integer/description/ ) | [ java/py/js] ( ./algorithms/ReverseInteger ) | Easy|
188
188
| 507| [ Perfect Number] ( https://leetcode-cn.com/problems/perfect-number/description/ ) | [ java/py/js] ( ./algorithms/PerfectNumber ) | Easy|
189
189
| 840| [ Magic Squares In Grid] ( https://leetcode-cn.com/problems/magic-squares-in-grid/description/ ) | [ java/py/js] ( ./algorithms/MagicSquaresInGrid ) | Easy|
190
+ | 532| [ K Diff Pairs In An Array] ( https://leetcode-cn.com/problems/k-diff-pairs-in-an-array/description/ ) | [ java/py/js] ( ./algorithms/KDiffPairsInAnArray ) | Easy|
190
191
191
192
# Database
192
193
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # K Diff Pairs In An Array
2
+ This problem is easy to solve by double pointers or hashmap
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int findPairs (int [] nums , int k ) {
3
+ if (k < 0 ) return 0 ;
4
+
5
+ int res = 0 ,
6
+ length = nums .length ,
7
+ i = 0 ,
8
+ j = 0 ;
9
+
10
+ Arrays .sort (nums );
11
+
12
+ while (i < length && j < length ) {
13
+ if (i >= j ) {
14
+ j ++;
15
+ } else if (nums [j ] - nums [i ] == k ) {
16
+ res ++;
17
+ int temp = nums [i ];
18
+ while (i < length && temp == nums [i ]) {
19
+ i ++;
20
+ }
21
+ } else if (nums [j ] - nums [i ] > k ) {
22
+ i ++;
23
+ } else if (nums [j ] - nums [i ] < k ) {
24
+ j ++;
25
+ }
26
+ }
27
+
28
+ return res ;
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @param {number } k
4
+ * @return {number }
5
+ */
6
+ var findPairs = function ( nums , k ) {
7
+ if ( k < 0 ) return 0
8
+
9
+ let parisMap = { } ,
10
+ valueMap = { } ,
11
+ res = 0
12
+
13
+ nums . forEach ( num => {
14
+ let p1 = num + k ,
15
+ p2 = num - k
16
+
17
+ if ( valueMap [ p1 ] ) {
18
+ let max = Math . max ( p1 , num ) ,
19
+ min = Math . min ( p1 , num )
20
+ if ( ! parisMap [ `${ max } x${ min } ` ] ) {
21
+ parisMap [ `${ max } x${ min } ` ] = true
22
+ res ++
23
+ }
24
+ }
25
+ if ( valueMap [ p2 ] ) {
26
+ let max = Math . max ( p2 , num ) ,
27
+ min = Math . min ( p2 , num )
28
+ if ( ! parisMap [ `${ max } x${ min } ` ] ) {
29
+ parisMap [ `${ max } x${ min } ` ] = true
30
+ res ++
31
+ }
32
+ }
33
+ valueMap [ num ] = true
34
+ } )
35
+
36
+ return res
37
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def findPairs (self , nums , k ):
3
+ """
4
+ :type nums: List[int]
5
+ :type k: int
6
+ :rtype: int
7
+ """
8
+ res = 0
9
+ length = len (nums )
10
+ nums .sort ()
11
+
12
+ i = j = 0
13
+ while j < length and i < length :
14
+ if i >= j :
15
+ j += 1
16
+ elif nums [j ] - nums [i ] == k :
17
+ res += 1
18
+ temp = nums [i ]
19
+ while i < length and temp == nums [i ]:
20
+ i += 1
21
+ elif nums [j ] - nums [i ] > k :
22
+ i += 1
23
+ elif nums [j ] - nums [i ] < k :
24
+ j += 1
25
+
26
+ return res
You can’t perform that action at this time.
0 commit comments