Skip to content

Commit 2cf731b

Browse files
committed
Create LeetCode_373_95.java
1 parent 0ddb566 commit 2cf731b

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Week_03/id_95/LeetCode_373_95.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class LeetCode_373_95 {
2+
public List<int[]> kSmallestPairs(int[] nums1, int[] nums2, int k) {
3+
List<int[]> res = new LinkedList<>();
4+
Queue<int[]> queue = new PriorityQueue<>(k,new Comparator<int[]>(){
5+
public int compare(int[] o1,int[] o2){
6+
int tmp1 = o1[0]+o1[1];
7+
int tmp2 = o2[0]+o2[1];
8+
9+
return tmp1 - tmp2;
10+
}
11+
});
12+
13+
for(int i = 0;i<nums1.length;i++){
14+
for(int j = 0;j<nums2.length;j++){
15+
queue.add(new int[]{nums1[i],nums2[j]});
16+
}
17+
}
18+
19+
while(k-->0){
20+
int[] tmp = queue.poll();
21+
if(tmp == null)
22+
break;
23+
res.add(tmp);
24+
}
25+
26+
return res;
27+
}
28+
}

0 commit comments

Comments
 (0)