给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k。
定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2。
找到和最小的 k 对数字 (u1,v1), (u2,v2) ... (uk,vk)。
示例 1:
给出: nums1 = [1,7,11], nums2 = [2,4,6],  k = 3
返回: [1,2],[1,4],[1,6]
返回序列中的前 3 对数:
[1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
示例 2:
给出:nums1 = [1,1,2], nums2 = [1,2,3],  k = 2
返回: [1,1],[1,1]
返回序列中的前 2 对数:
[1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
示例 3:
给出:nums1 = [1,2], nums2 = [3],  k = 3
返回: [1,3],[2,3]
也可能序列中所有的数对都被返回:
[1,3],[2,3]
详见:https://leetcode.com/problems/find-k-pairs-with-smallest-sums/description/

C++:

class Solution {
public:
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k)
{
vector<pair<int, int>> res;
for (int i = 0; i < min((int)nums1.size(), k); ++i)
{
for (int j = 0; j < min((int)nums2.size(), k); ++j)
{
res.push_back({nums1[i], nums2[j]});
}
}
sort(res.begin(), res.end(), [](pair<int, int> &a, pair<int, int> &b){return a.first + a.second < b.first + b.second;});
if (res.size() > k)
{
res.erase(res.begin() + k, res.end());
}
return res;
}
};

参考:http://www.cnblogs.com/grandyang/p/5653127.html

373 Find K Pairs with Smallest Sums 查找和最小的K对数字的更多相关文章

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

  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】373. Find K Pairs with Smallest Sums 解题报告(Python)

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

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

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

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

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

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

随机推荐

  1. Java日期LocalDate使用

    在做报表统计时,需要对指定时间内的数据做统计,则需要使用到时间日期API 在此使用的是java.util.Date的完美私生子LocalDate类 LocalDate方法介绍 now() : 从默认时 ...

  2. 遍历ArrayList数组时可能存在的问题

    我们都知道ArrayList类中有个重要的方法是Add(),该方法用于向集合中添加元素,它有一个object类型的参数,表示通过该方法可以向集合中添加任意类型的项,由于ArrayList动态数组中的元 ...

  3. 【SQL Server 学习系列】-- SQL查询数据库表字段值不为空或Null的所有列

    ) set @TableName = 'Agency' -- 表名 declare @querySql nvarchar(max) set @querySql = 'select ' ) declar ...

  4. openstack setup demo 前言

    我们搭建一套三节点的openstanck集群.一个controller节点,两个compute节点.操作系统采用Centos7,操作系统版本信息如下. [root@controller01 ~]# c ...

  5. 我的arcgis培训照片8

    来自:http://www.cioiot.com/successview-554-1.html

  6. C++之桟的应用---括号匹配

    刚開始学习数据结构.用桟写了一个经典的应用,括号匹配. 算法思路: 输入字符串时.将 '(' , '['  压入桟.遇到 ')'  ']'  时,再栈顶出桟.进行括号匹配.假设成功匹配.则继续进行.否 ...

  7. C#字符串数组排序 C#排序算法大全 C#字符串比较方法 一个.NET通用JSON解析/构建类的实现(c#) C#处理Json文件 asp.net使用Jquery+iframe传值问题

    C#字符串数组排序   //排序只带字符的数组,不带数字的 private   string[]   aa   ={ "a ", "c ", "b & ...

  8. 获取Linux磁盘分区的UUID

    在设置fstab自动挂载时,需要填写如下的信息: # <file system> <mount point> <type> <options> < ...

  9. java图片处理工具之-ImageMagick+jmagick(二)

    简单的图片处理測试类: public class ImageUtil { static{           System.setProperty("jmagick.systemclassl ...

  10. C#.NEt-GDI+中的Pen測试

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...