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. GridView控件显示图片

    与图片的二进制数据库存储和显示 1.将图片以二进制存入数据库 2.读取二进制图片在页面显示 3.设置Image控件显示从数据库中读出的二进制图片 4.GridView中ImageField以URL方式 ...

  2. Windows 环境下基于 nginx 的本地 PyPI 源

    Windows 环境下基于 nginx 的本地 PyPI 源的搭建: 1.登录 nginx 官网,下载安装包

  3. 【转】MyEclipse8.5集成Tomcat7时的启动错误:Exception in thread “main” java.lang.NoClassDefFoundError org/apache/commons/logging/LogFactory

    http://www.cnblogs.com/newsouls/p/4021198.html 今天,安装Tomcat7.0.21后,单独用D:\apache-tomcat-7.0.21\bin\sta ...

  4. linux centos6 NAT 端口转发

    有很多时候我们为了安全,需要将例如数据库服务器放到内网中.但是有些时候又系统给外网开一个端口,这时就可以利用外网的服务器进行一个端口转发.今天我们以centos6 为例进行端口转发配置. 首先vi / ...

  5. iOS开发——数据持久化Swift篇&通用文件存储

    通用文件存储 import UIKit class ViewController: UIViewController { @IBOutlet weak var textField: UITextFie ...

  6. ulimit 命令详解

      Linux对于每个用户,系统限制其最大进程数.为提高性能,可以根据设备资源情况,设置各linux 用户的最大进程数 可以用ulimit -a 来显示当前的各种用户进程限制. 下面我把某linux用 ...

  7. HTTP 无法注册 URL http://+:12345/HelloWcfService/。进程不具有此命名空间的访问权限

    运行的时候却发现出如下问题:HTTP 无法注册 URLhttp://+:12345/HelloWcfService/.进程不具有此命名空间的访问权限(有关详细信息,请参见http://go.micro ...

  8. 用antlr文法编写的hermit swrl规则(分享)

    /* * To change this license header, choose License Headers in Project Properties. * To change this t ...

  9. js中点击事件方法三种方式的区别

    在javascript中,可以为某个元素指定事件,指定的方式有以下三种: 1.在html中,使用onclick属性 2.在javascript中,使用onclick属性 (1)注意函数名没有双引号. ...

  10. 封装JDBC操作数据库的方法

    自己动手封装java操作数据库的方法: 一:目录结构 二:所需依赖的第三方jar包 这里只需引入mysql-connector-java-5.1.8-bin.jar,mysql数据库驱动jar包 三: ...