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

给定两个以升序排列的整形数组 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

原文地址:https://www.cnblogs.com/xidian2014/p/8848219.html

时间: 2024-10-14 17:48:30

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

373. Find K Pairs with Smallest Sums

/* * 373. Find K Pairs with Smallest Sums * 2016-7-16 by Mingyang * heap来做,记住Queue<int[]>不是Queue<Integer[]>因为array不是primitive variable * 另外一点就是k万一很大也不行,大于heap的size就只能输出那么多 * 判断空,不要想太多,什么一边为空一边不为空的 */ public static List<int[]> kSmallestPa

Leetcode 373. Find K Pairs with Smallest Sums

373. Find K Pairs with Smallest Sums Total Accepted: 1453 Total Submissions: 5789 Difficulty: Medium You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define a pair (u,v) which consists of one element from t

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. Define a pair (u,v) which consists of one element from the first array and one element from the second array. Find the k pairs (u1,v1),(u2,v2) ...(uk,vk)

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 a pair (u,v) which consists of one element from the first array and one element from the second array. Find the k pairs (u1,v1),(u2,v2) ...(uk,v

[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 a pair (u,v) which consists of one element from the first array and one element from the second array. Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) wit

LeetCode Find K Pairs with Smallest Sums

原题链接在这里:https://leetcode.com/problems/find-k-pairs-with-smallest-sums/ 题目: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define a pair (u,v) which consists of one element from the first array and one ele

Find K Pairs with Smallest Sums -- LeetCode

You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define a pair (u,v) which consists of one element from the first array and one element from the second array. Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) wit

Find K Pairs with Smallest Sums 解答

Question You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define a pair (u,v) which consists of one element from the first array and one element from the second array. Find the k pairs (u1,v1),(u2,v2) ...(u

Find K Pairs with Smallest Sum

1 public class Solution { 2 private Set<Integer> visited; 3 public List<int[]> kSmallestPairs(int[] nums1, int[] nums2, int k) { 4 List<int[]> result = new ArrayList<>(); 5 visited = new HashSet<>(); 6 PriorityQueue<int[]&