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

给你两个数组nums1和nums2,这两个数组都是递增排列的,还给你一个整数k。

Define a pair (u,v) which consists of one element from the first array and one element from the second array.

定义一个pair(u,v)其中一个数是第一个数组中的,另一个数十第二个数组中的。

Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) with the smallest sums.

找出在所有pair中两数和由小到大排列的前k个pair。

Example 1:

Given nums1 = [1,7,11], nums2 = [2,4,6],  k = 3

Return: [1,2],[1,4],[1,6]

The first 3 pairs are returned from the sequence:
[1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]

Example 2:

Given nums1 = [1,1,2], nums2 = [1,2,3],  k = 2

Return: [1,1],[1,1]

The first 2 pairs are returned from the sequence:
[1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]

Example 3:

Given nums1 = [1,2], nums2 = [3],  k = 3 

Return: [1,3],[2,3]

All possible pairs are returned from the sequence:
[1,3],[2,3]
 class Solution {
public:
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
vector<pair<int,int>> ret;
if(nums1.empty()||nums2.empty()) return ret;
int l1=nums1.size(),l2=nums2.size();
vector<int> mm(l1,);
for(int count=,low=;count<k&&mm[l1-]<l2;){
int tmpi=low,tmpms=nums1[low]+nums2[mm[low]];
for(int i=low+;i<l1;i++){
if(mm[i]==l2)continue;
int tmp=nums1[i]+nums2[mm[i]];
if(tmp<tmpms){tmpms=tmp;tmpi=i;}
if(mm[i]==)break;
}
ret.push_back(pair<int,int>(nums1[tmpi],nums2[mm[tmpi]]));
mm[tmpi]++;
if(mm[low]==l2)low++;
count++;
}
return ret;
}
};

上面是AC代码,不太会写解释,还麻烦,有问题可以消息我。

373. 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. 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 ...

  3. 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 ...

  4. #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 ...

  5. [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 ...

  6. [LC] 373. Find K Pairs with Smallest Sums

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

  7. 373 Find K Pairs with Smallest Sums 查找和最小的K对数字

    给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k.定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2.找到和最小的 k 对数字 (u1,v1 ...

  8. [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 ...

  9. Leetcode Find K Pairs with smallest sums

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

随机推荐

  1. JavaScript要点 (六) 函数参数

    JavaScript 函数对参数的值没有进行任何的检查. 函数显式参数(Parameters)与隐式参数(Arguments) 函数的显式参数: functionName(parameter1, pa ...

  2. NOTES : A Model of Gas Exchange for Hyperpolarized Xe(129) Magnetic Resonance of the Lung

    NOTES :  A Model of Gas Exchange for Hyperpolarized Xe(129) Magnetic Resonance of the Lung  背景知识: Ga ...

  3. multiselect获取选中的多个下拉项的值(逗号分割的字符串)

    /*传入下拉select标签*/ function get_selected(mslt_employees) { var emplo =mslt_employees.multiselect(" ...

  4. CPU相关信息

    unit untCpuInfo;interface{ 获取 CPU 制造商 }function GetCpuFactory: String;{ 获取 CPU 家族系统 }function GetCpu ...

  5. 03---JavaScript基础整理

    一.概述:           Netscape开发的一种基于对象和事件驱动的脚本语言.                  被设计用来想HTML页面添加交互行为.           无需编译,可由浏 ...

  6. AIR 移动设备上的存储控制

    File.documentsDirectory, File.userDirectory, File.desktopDirectory 等.可以保存大的数据,如图片,视屏,和临时文件.访问这些文件的全选 ...

  7. careercup-C和C++ 13.6

    13.6 基类的析构函数为何要声明为virtual? 解答: 用对象指针来调用一个函数,有以下两种情况: 如果是虚函数,会调用派生类中的版本. 如果是非虚函数,会调用指针所指类型的实现版本. 析构函数 ...

  8. 实例源码--Android通讯录源码

      下载源码   技术要点: 1.通讯录联 系人的管理 2.接听.打电话 3.发短信 4. 源码带详细的 中文注释    ...... 详细介绍: 1.通讯录联系人的管理 播放器具有播放本地音乐的功能 ...

  9. windows 7 共享,未授予用户在此计算机上的请求登录类型

    刚刚重装了windows7,新下载的一个ghost版本,结果却不能共享,每次访问这台机器的共享都提示, 未授予用户在此计算机上的请求登录类型 这个情况好像是存在于win7访问win7,我用一台XP系统 ...

  10. python--列表的使用

    1.定义列表: names = ['Mo',"Tenglan",'Eric'] 通过下标访问列表中的元素,下标从0开始计数 >>> names[0] 'Mo' & ...