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. Struts的类型转换

    struts中jsp提交的数据,struts会自动转换为action中的属性类型: 对于基本的数据类型以及日期类型会自动转换: 就像前面那一篇博客的代码封装一样,会把字符串类型传过来的数据转化为相应的 ...

  2. mysql 在线修改表结构工具 gh-ost

    gh-ost使用测试: gh-ost -host='192.168.65.136' -user=root -password='' -database='haha' -chunk-size=10000 ...

  3. 解决下载Android Build-tools 19.1.0失败

    准备从Eclipse转到Android Studio了.今天尝试Android Studio的时候,被它提醒我SDK的Android Build-tools版本过低,需要升级. 于是打开Android ...

  4. Smack[3]用户列表,头像,组操作,用户操作

    用户列表 Smack主要使用Roster进行列表管理的 connection.getRoster(); /** * 返回所有组信息 <RosterGroup> * * @return Li ...

  5. cocos2d-x NotificationCenter

    转自:http://www.xinze.me/cocos2d-x-ccnotificationcenter/ 在前端开发中,JS和as3中都有很好的监听机制, 我们使用Event的addEventLi ...

  6. Select-Object用法

    展开 modules 属性的详细信息 get-process |? {$_.processname -eq "cmd"}|select -ExpandProperty module ...

  7. HDU 4608 I-number(模拟)

    I-number Time Limit: 5000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 The I-number of x is defined t ...

  8. Rational Rose--简介

    Rational Rose Rational Rose是Rational公司出品的一种面向对象的统一建模语言的可视化建模工具.用于可视化建模和公司级水平软件应用的组件构造. 目前版本的Rational ...

  9. Java算法实例集合(2)

    这是Standford一位计算机老师的私藏,里面包含了不少Java/C++的算法实现代码.有兴趣的朋友可以看看.

  10. Java对数组的操作(二)——集合与数组的切换

    在Java开发中经常遇见集合与数组的互相切换,怎样实现呢,呵呵呵,非常easy: import java.util.ArrayList; import java.util.Arrays;       ...