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:

  1. The value k is positive and will always be smaller than the length of the sorted array.
  2. Length of the given array is positive and will not exceed 104
  3. 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]的更多相关文章

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

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

  3. 【LeetCode】658. Find K Closest Elements 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/find-k-c ...

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

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

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

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

  8. C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

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

随机推荐

  1. 【Django笔记二】Django2.0配置模板和静态文件

    一.环境版本信息: 操作系统:windows10 Django版本:2.0.5 Python版本:3.6.4 二.创建模板 1.在my_project文件夹下新建文件夹templates用于存放模板文 ...

  2. C语言的乱七八糟

    Note For C Linux下C编程基础(gcc/gdb/make使用) 一.vi学习 二.初探emacs 三.gcc编译器 3.1 gcc所支持后缀名解释 后缀名 解释 后缀名 解释 .c C原 ...

  3. Linux中Zookeeper部署和集群部署

    自己网上下载安装包,我下载的是tar.gz安装包直接解压,也可以下载rpm格式 1.下载zookeeper安装包,放到/usr/local/zookeeper安装包网上下载 2.解压文件tar -zx ...

  4. MapReduce之Map Join

    一 介绍 之所以存在Reduce Join,是因为在map阶段不能获取所有需要的join字段,即:同一个key对应的字段可能位于不同map中.Reduce side join是非常低效的,因为shuf ...

  5. 虚拟机搭建hadoop的步骤

    1.首先是安装Vmware Workstation,下载地址:https://www.vmware.com/products/workstation-player/workstation-player ...

  6. SaltStack error: No module named 'salt'

    启动saltstack的时候出现下面的错误 问题原因 是因为我在centos7中安装了多版本的python导致的 解决方案 将文件下面文件首行更改成python2 [root@saltstack-12 ...

  7. 转载:C语言指针使用的注意事项

    相信大家对指针的用法已经很熟了,这里也不多说些定义性的东西了,只说一下指针使用中的注意事项吧. 一.在定义指针的时候注意连续声明多个指针时容易犯的错误,例如int * a,b;这种声明是声明了一个指向 ...

  8. c语言杨氏矩阵算法

    杨氏矩阵 有一个二维数组.数组的每行从左到右是递增的,每列从上到下是递增的.在这样的数组中查找一个数字是否存在.时间复杂度小于O(N);数组:1 2 32 3 43 4 5 1 3 42 4 54 5 ...

  9. python2.7入门---CGI编程&文件上传&文件下载

        这次我们来看下文件下载和上传的操作.首先是上传,HTML设置上传文件的表单需要设置 enctype 属性为 multipart/form-data,代码如下所示: <!DOCTYPE h ...

  10. 笔记-python-float(‘inf’)

    笔记-python-float(‘inf’) 看算法时发现了flaot(‘inf’). Python中可以用如下方式表示正负无穷: float("inf"), float(&quo ...