// https://discuss.leetcode.com/topic/50527/java-10ms-solution-no-priority-queue

class Solution {
public:
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end()); vector<int> buf1(nums1.size(), );
vector<pair<int, int>> vpair;
int minV = INT_MAX;
int tmpV = -; while (vpair.size() < k) {
for (int i=; i<nums1.size(); ++i) {
if (buf1[i] < nums2.size() && nums1[i] + nums2[buf1[i]] < minV) {
minV = nums1[i] + nums2[buf1[i]];
tmpV = i;
}
} if (tmpV != -) {
vpair.push_back(make_pair(nums1[tmpV], nums2[buf1[tmpV]]));
buf1[tmpV] = buf1[tmpV] + ;
minV = INT_MAX;
tmpV = -;
}
else {
break;
} } return vpair;
}
};

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. [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 Find K Pairs with smallest sums

    本题的特点在于两个list nums1和nums2都是已经排序好的.本题如果把所有的(i, j)组合都排序出来,再取其中最小的K个.其实靠后的很多组合根本用不到,所以效率较低,会导致算法超时.为了简便 ...

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

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

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

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

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

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

随机推荐

  1. react篇章-React State(状态)-将生命周期方法添加到类中

    将生命周期方法添加到类中 在具有许多组件的应用程序中,在销毁时释放组件所占用的资源非常重要. 每当 Clock 组件第一次加载到 DOM 中的时候,我们都想生成定时器,这在 React 中被称为挂载. ...

  2. SQL注入使用Django中继数据包bypassWAF

    原理 本人基于文章bypassword的文章在HTTP协议层面绕过WAF所编写一款工具. 环境 Python3.7.0 Django 2.1 Requests 使用范围 POST注入 可以分块传输的漏 ...

  3. 五、django rest_framework源码之版本控制剖析

    1 绪论 Djangorest_framework的版本控制允许用户更改不同客户端之间的行为,且提供了许多不同的版本控制方案.版本控制由传入的客户端请求确定,可以基于请求URL,也可以基于请求标头. ...

  4. C++实现平衡二叉树

    1.概念 平衡二叉树(AVL Tree)首先要满足二叉树的定义,如下 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: 若左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若右子树不空, ...

  5. Moo University - Financial Aid POJ 2010 优先队列(最大堆)

    题目:http://poj.org/problem?id=2010 题目大意: 奶牛上大学.因为经济问题,每头奶牛都需要一定的补助需求,学校会提供一定的资金用于补助 每头牛都有自己的分数,学校招收的名 ...

  6. 机器学习之路:python k均值聚类 KMeans 手写数字

    python3 学习使用api 使用了网上的数据集,我把他下载到了本地 可以到我的git中下载数据集: https://github.com/linyi0604/MachineLearning 代码: ...

  7. 【Codeforces 498B】 B. Name That Tune (概率DP)

    B. Name That Tune time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  8. spring---aop(7)---Spring AOP中expose-proxy介绍

    写在前面 expose-proxy.为是否暴露当前代理对象为ThreadLocal模式. SpringAOP对于最外层的函数只拦截public方法,不拦截protected和private方法(后续讲 ...

  9. cloc 统计代码行数工具

    cloc 统计代码行数工具 官网地址:http://cloc.sourceforge.net/ 下载完成后 会生成一个.exe文件 需要修改文件名为 cloc.exe 然后把这个文件拷贝到需要统计的根 ...

  10. Tasker 模拟重复点击按钮

    http://stackoverflow.com.80bola.com/questions/21544271/android-tasker-app-advance-input-shell-comman ...