给定两个以升序排列的整形数组 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. php-fpm配置项

    php版本:php-7.3 全局配置 ;;;;;;;;;;;;;;;;;;;;; ; FPM Configuration ; ;;;;;;;;;;;;;;;;;;;;; ; All relative ...

  2. MongoDB小结09 - update【定位修改器】

    如果要操作数组中的值,可以用值在数组中的位置当做参数来删除 db.user.update({"name":"codingwhy.com"},{"$se ...

  3. 在 IIS 中承载 WCF 服务

    本主题概述了创建 Internet 信息服务 (IIS) 中承载的 Windows Communication Foundation (WCF) 服务所需的基本步骤. 本主题假设您熟悉 IIS 且了解 ...

  4. some 算法

    矩阵变换:: 请用一条语句将: arr = [[1, 2, 3, 'a'], [4, 5, 6, 'b'], [7, 8, 9, 'c']] 转换装置矩阵为: [[1, 4, 7], [2, 5, 8 ...

  5. mysql设置datetime默认值为now

    mysql设置datetime默认值为now MYSQL-TIMESTAMP数据类型的默认值与自动更新问题 http://blog.csdn.net/scogeek/article/details/5 ...

  6. Outlook2010 没有Exchange Server,怎么自动回复邮件?

    步骤 1:创建邮件模板 单击“开始”>“新建邮件”. 在邮件正文中,键入要作为自动答复发送的邮件. 在邮件窗口中,单击“文件”>“另存为”. 在“另存为”对话框中的“保存类型”列表中,单击 ...

  7. C# .NET 如何批量增加或取消注释

    选中一批文本之后 批量增加注释:Ctrl+K Ctrl+C 批量取消注释:Ctrl+K Ctrl+U  

  8. AutoCAD如何倒角 倒圆角 倒直角

    倒圆角:输f 再输r 再输入你想倒的半径,然后选相邻的两边倒直角:输chamfer 再输d 再输你想倒的距离,然后先相邻的两边 祝你成功

  9. 连通分量模板:tarjan: 求割点 &amp;&amp; 桥 &amp;&amp; 缩点 &amp;&amp; 强连通分量 &amp;&amp; 双连通分量 &amp;&amp; LCA(近期公共祖先)

    PS:摘自一不知名的来自大神. 1.割点:若删掉某点后.原连通图分裂为多个子图.则称该点为割点. 2.割点集合:在一个无向连通图中,假设有一个顶点集合,删除这个顶点集合,以及这个集合中全部顶点相关联的 ...

  10. UGUI 实现Button长按效果(RepeatButton)

    Tag:加入了一个延迟,在button按下状态一段时间后再開始 repeate using UnityEngine; using UnityEngine.Events; using UnityEngi ...