[leetcode-658-Find K Closest Elements]
Given a sorted array, two integers k
and x
, find the k
closest elements to x
in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred.
Example 1:
Input: [1,2,3,4,5], k=4, x=3
Output: [1,2,3,4]
Example 2:
Input: [1,2,3,4,5], k=4, x=-1
Output: [1,2,3,4]
Note:
- The value k is positive and will always be smaller than the length of the sorted array.
- Length of the given array is positive and will not exceed 104
- Absolute value of elements in the array and x will not exceed 104
思路:
用一个map记录数组中的值距离x的大小,利用map有序的特性。
int SIZE = arr.size();
map<int, vector<int>> m;
for(int i = ; i < SIZE; ++i) {
int val = arr[i];
m[abs(val - x)].push_back(val);
}
vector<int> ans;
auto it = m.begin();
while(ans.size() < k) {
vector<int> &v = it->second;
sort(v.begin(), v.end());
int i = ;
while(i < v.size() && k > ans.size()) {
ans.push_back(v[i++]);
}
++it;
}
sort(ans.begin(), ans.end());
return ans;
vector<int> findClosestElements(vector<int>& arr, int k, int x)
{
vector< int > ret;
vector< int > cur;
auto it = lower_bound( arr.begin(), arr.end(), x );//低
//cout << *it << endl; long long sum = ;
long long min_val = 0xc0c0c0c0;
auto it_start = ( it - k < arr.begin() ) ? arr.begin() : it-k;
auto it_end = ( it > arr.end() - k ) ? arr.end() - k : it; //cout << *it_start << endl;
//cout << *it_end << endl; for( auto it_cur = it_start; it_cur <= it_end; it_cur++ )
{
sum = ;
cur.clear();
for( int i = ; i < k; i++ )
{
cur.push_back( *(it_cur+i) );
sum += abs( ( *(it_cur+i) - x ) );
}
if( sum < min_val )
{
min_val = sum;
swap( ret, cur );
}
} return ret;
}
[leetcode-658-Find K Closest Elements]的更多相关文章
- [LeetCode] 658. Find K Closest Elements 寻找K个最近元素
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
- [leetcode]658. Find K Closest Elements绝对距离最近的K个元素
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
- 【LeetCode】658. Find K Closest Elements 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/find-k-c ...
- 658. Find K Closest Elements
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
- [LeetCode] Find K Closest Elements 寻找K个最近元素
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
- LeetCode - Find K Closest Elements
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
- [Swift]LeetCode658. 找到 K 个最接近的元素 | Find K Closest Elements
Given a sorted array, two integers k and x, find the kclosest elements to x in the array. The result ...
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- Find K Closest Elements
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
随机推荐
- Git&GitHub-初步使用
Git 1.安装 下载安装包,安装,默认安装了 Git GUI Here 和 Git Bash Here. 需要在哪里使用 git,只需在文件夹空白处右键,选择Git Bash Here即可打开 gi ...
- 关于nodejs下载组件经常失败的问题
由于最近在刚开始做一个前台element和mybatisplus的项目,但是在使用nodejs下载vue的脚手架和各种组件时,会经常出现下载失败的问题,进而导致前台无法启动. 在网上查询之后发现在下载 ...
- percona数据库监控工具的安装部署
Percona Monitoring and Management 安装 PMM是一个开源,免费的mysql管理监控平台,他可以用来分析mysql,mariadb和mongodb的服务器性能. 安装步 ...
- Django中间件执行顺序
中间件 Django中的中间件是一个轻量级.底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出.中间件的设计为开发者提供了一种无侵入式的开发方式,增强了Django ...
- 使用bareos备份
官方文档http://doc.bareos.org/master/html/bareos-manual-main-reference.html bareos主要由主控端和客户端构成: 包含的重要的服务 ...
- .Net Core如何在任意位置获取配置文件的内容
前几天群里有人问,我想在程序里的任意位置读取appsetting.json里的配置,该怎么搞. 话不多说上源码 首先,要想读取配置文件我们要用到IConfiguration 接口,这个接口在Start ...
- Java : Netty 入门案例
接收端代码: public class IOServer { public static void main(String[] args) throws IOException, Interrupte ...
- buck型DC-DC分析
BUCK型DC/DC电源分析 这种buck型DC/DC电路的拓扑结构:( 1N5822叫续流二极管!) LM2756相当于高速断开和闭合的开关,连接在Vin与Vout脚之间. 1. 在开关闭合时,电流 ...
- MongoDB入门---聚合操作&管道操作符&索引的使用
经过前段时间的学习呢,我们对MongoDB有了一个大概的了解,接下来就要开始使用稍稍深入一点的东西了,首先呢,就是MongoDB中的聚合函数,跟mysql中的count等函数差不多.话不多说哈,我们先 ...
- WPF Prism MVVM 中 弹出新窗体. 放入用户控件
原文:WPF Prism MVVM 中 弹出新窗体. 放入用户控件 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_37214567/artic ...