[leetcode-373]Find K Pairs with Smallest Sums(java)
来源:互联网 发布:浪潮大数据公司 编辑:程序博客网 时间:2024/05/16 14:33
思路:这道题就是简单地利用最小堆模型来完成。
代码如下:
public class Solution { public List<int[]> kSmallestPairs(int[] nums1, int[] nums2, int k) { List<int[]> res = new LinkedList<>(); Queue<int[]> queue = new PriorityQueue<>(k,new Comparator<int[]>(){ public int compare(int[] o1,int[] o2){ int tmp1 = o1[0]+o1[1]; int tmp2 = o2[0]+o2[1]; return tmp1 - tmp2; } }); for(int i = 0;i<nums1.length;i++){ for(int j = 0;j<nums2.length;j++){ queue.add(new int[]{nums1[i],nums2[j]}); } } while(k-->0){ int[] tmp = queue.poll(); if(tmp == null) break; res.add(tmp); } return res; }}
0 0
- leetcode 373 Find K Pairs with Smallest Sums java
- [leetcode-373]Find K Pairs with Smallest Sums(java)
- 【Leetcode】Find K Pairs with Smallest Sums
- leetcode Find K Pairs with Smallest Sums
- leetcode Find K Pairs with Smallest Sums
- LeetCode: Find K Pairs with Smallest Sums
- Leetcode: Find K Pairs with Smallest Sums
- leetcode-Find K Pairs with Smallest Sums
- LeetCode 373 Find K Pairs with Smallest Sums
- LeetCode[373] Find K Pairs with Smallest Sums
- leetcode:heap:Find K Pairs with Smallest Sums(373)
- Leetcode 373 Find K Pairs with Smallest Sums
- leetcode 373. Find K Pairs with Smallest Sums
- LeetCode—373. Find K Pairs with Smallest Sums
- [leetcode] 373. Find K Pairs with Smallest Sums
- [leetcode] 373. Find K Pairs with Smallest Sums 解题报告
- leetcode.373. Find K Pairs with Smallest Sums
- LeetCode 373. Find K Pairs with Smallest Sums
- 混合开发的大趋势之一React Native(Hello Horld+简单ViewGroup)
- XZ_iOS之SourceTree点击“推送”时用户名填写错误
- Nested Hypervisor: 公有云迁移技术
- url encoding
- json传递数据问题
- [leetcode-373]Find K Pairs with Smallest Sums(java)
- java web笔记——软件三层结构&MVC模式实现注册登陆案例
- ios网络操作
- 面向过程处理机制VS面向对象处理机制
- PS的一些快捷键
- Tkinter教程之Frame篇
- 分模块开发完美衔接小知识
- ios基础控件UILabel
- java内省入门