File tree Expand file tree Collapse file tree 1 file changed +7
-11
lines changed Expand file tree Collapse file tree 1 file changed +7
-11
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public List <List <Integer >> kSmallestPairs (int [] nums1 , int [] nums2 , int k ) {
3
- if (nums1 . length == 0 || nums2 .length == 0 || k == 0 ) {
3
+ if (nums1 == null || nums2 == null || nums1 .length == 0 || nums2 . length == 0 ) {
4
4
return Collections .emptyList ();
5
5
}
6
6
7
7
List <List <Integer >> result = new ArrayList <>();
8
- PriorityQueue <int []> pq = new PriorityQueue <>((a , b ) -> (a [0 ] + a [1 ]) - (b [0 ] + b [1 ]));
8
+ PriorityQueue <int []> pq = new PriorityQueue <int [] >((p1 , p2 ) -> (p1 [0 ] + p1 [1 ]) - (p2 [0 ] + p2 [1 ]));
9
9
10
10
for (int i = 0 ; i < nums1 .length ; i ++) {
11
11
pq .offer (new int [] { nums1 [i ], nums2 [0 ], 0 });
12
12
}
13
13
14
14
for (int i = 0 ; i < Math .min (nums1 .length * nums2 .length , k ); i ++) {
15
- int [] curr = pq .poll ();
15
+ int [] pair = pq .poll ();
16
16
17
- List <Integer > temp = new ArrayList <>();
18
- temp .add (curr [0 ]);
19
- temp .add (curr [1 ]);
17
+ result .add (Arrays .asList (pair [0 ], pair [1 ]));
20
18
21
- result .add (temp );
22
-
23
- if (curr [2 ] < nums2 .length - 1 ) {
24
- int idx = curr [2 ] + 1 ;
25
- pq .offer (new int [] { curr [0 ], nums2 [idx ], idx });
19
+ if (pair [2 ] < nums2 .length - 1 ) {
20
+ int nextIdx = pair [2 ] + 1 ;
21
+ pq .offer (new int [] { pair [0 ], nums2 [nextIdx ], nextIdx });
26
22
}
27
23
}
28
24
You can’t perform that action at this time.
0 commit comments