1. // https://discuss.leetcode.com/topic/50527/java-10ms-solution-no-priority-queue
  2.  
  3. class Solution {
  4. public:
  5. vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
  6. sort(nums1.begin(), nums1.end());
  7. sort(nums2.begin(), nums2.end());
  8.  
  9. vector<int> buf1(nums1.size(), );
  10. vector<pair<int, int>> vpair;
  11. int minV = INT_MAX;
  12. int tmpV = -;
  13.  
  14. while (vpair.size() < k) {
  15. for (int i=; i<nums1.size(); ++i) {
  16. if (buf1[i] < nums2.size() && nums1[i] + nums2[buf1[i]] < minV) {
  17. minV = nums1[i] + nums2[buf1[i]];
  18. tmpV = i;
  19. }
  20. }
  21.  
  22. if (tmpV != -) {
  23. vpair.push_back(make_pair(nums1[tmpV], nums2[buf1[tmpV]]));
  24. buf1[tmpV] = buf1[tmpV] + ;
  25. minV = INT_MAX;
  26. tmpV = -;
  27. }
  28. else {
  29. break;
  30. }
  31.  
  32. }
  33.  
  34. return vpair;
  35. }
  36. };

find-k-pairs-with-smallest-sums的更多相关文章

  1. 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)

    [LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...

  2. [LeetCode] Find K Pairs with Smallest Sums 找和最小的K对数字

    You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...

  3. Leetcode Find K Pairs with smallest sums

    本题的特点在于两个list nums1和nums2都是已经排序好的.本题如果把所有的(i, j)组合都排序出来,再取其中最小的K个.其实靠后的很多组合根本用不到,所以效率较低,会导致算法超时.为了简便 ...

  4. 373. Find K Pairs with Smallest Sums

    You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. 给你两个数组n ...

  5. [Swift]LeetCode373. 查找和最小的K对数字 | Find K Pairs with Smallest Sums

    You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...

  6. 373. Find K Pairs with Smallest Sums 找出求和和最小的k组数

    [抄题]: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. D ...

  7. 373. Find K Pairs with Smallest Sums (java,优先队列)

    题目: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Def ...

  8. #Leetcode# 373. Find K Pairs with Smallest Sums

    https://leetcode.com/problems/find-k-pairs-with-smallest-sums/ You are given two integer arrays nums ...

  9. Find K Pairs with Smallest Sums -- LeetCode

    You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...

  10. [LeetCode] 373. Find K Pairs with Smallest Sums 找和最小的K对数字

    You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...

随机推荐

  1. Python入门1(简介、安装)

    第一次写博客,希望自己能养成每天一篇的好习惯(表示有很大的挑战,就不立Flag了) 先从Python开始吧.下面是本人所学习的主要教材电子版: 链接:http://pan.baidu.com/s/1e ...

  2. 内核启动文件系统后第一个执行的文件(inittab启动脚本分析)

    Linux 开机脚本启动顺序: 第一步:启动内核 第二步:执行init (配置文件/etc/inittab) 第三步:启动相应的脚本,执行inittab脚本,并且执行里面的脚本/etc/init.d ...

  3. Revit二次开发示例:APIAppStartup

    下面介绍一个在Revit启动和关闭时调用外部程序的例子.   Revit调用的dll主程序: using System; using System.Collections.Generic; using ...

  4. Linux驱动程序中的并发控制

    <临界区> a:对共享资源进行访问的代码称为临界区.   <原子操作>     a:原子操作用于执行轻量级,仅仅执行一次的的操作比如修改计数器,有条件的增加值,设置某一位.所谓 ...

  5. [BZOJ4820][SDOI2017]硬币游戏(高斯消元+KMP)

    比较神的一道题,正解比较难以理解. 首先不难得出一个(nm)^3的算法,对所有串建AC自动机,将在每个点停止的概率作为未知数做高斯消元即可. 可以证明,AC自动机上所有不是模式串终止节点的点可以看成一 ...

  6. 【2016NOIP十连测】【test4】【状压DP】【容斥原理】巨神兵

    题目大意: 给一个n个点(n<=17),m条边的有向图(无自环.无重边),求其无环子图的方案数. 题解: 看到n<=17,显然是用状压dp. 用f[i]表示点集i的满足条件的方案数. 状态 ...

  7. android实现gif图播放、暂停、继续播放

    之前做过一个项目,在android上实现gif图的播放以及点击屏幕弹出窗口显示gif图片的暂停,之前一直用gifView的jar包实现gif图片的显示,但是在gif暂停.继续播放这块没有找到好的解决方 ...

  8. 如何让浮动的元素换行??css

    当你想要做成这种布局效果的时候 紫色框里面的内容那样 它是一个列表 li元素是块级元素  默认大小是父元素ul的宽 并且换行 如果li没有背景的话那就不用管了 可是问题来了它不但有背景 而且是根据文字 ...

  9. 该死的Ubuntu 16.04不自动续租DHCP的IP

    BUG,这是一个BUG,参考:https://bugs.launchpad.net/ubuntu/+source/isc-dhcp/+bug/1551351,如果不自动续租IP,导致的问题就是网线灯还 ...

  10. Struts2 JSONObject的使用

    一.jar包 使用之前必须引入所须要的jar包,这里包含Struts2和JSONObject各自所必须的 Struts2: commons-fileupload-1.2.1.jarcommons-io ...