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. 网站程序application, session, cookie区别的一点小总结

    cookie:是存放于本地的文件,可以设置期限,一般来讲是比较小文本或键值对等,主要用于记录用户浏览信息,账号密码等等.可以让人们的登录变得方便,因为有了cookie,在一段时间内就可以自动登录以前所 ...

  2. BufPay.com 个人收款接口 接入步骤

    作为独立开发者产品需要收款是非常麻烦的,注册公司维护成本太高,市面上各种收款工具要么手续费太高,要么到账很慢,体验很不好. 看到 「BufPay.com 个人收款」 这个收款工具,挺有意思的.原理是监 ...

  3. VMware Workstation 安装Vmware tools 是 出现vmware tools unavailable

    这个问题是因为虚拟机安装的时候操作系统选择的不对,在Virtual Machine Settings中选择Options,在General中选择正确的操作系统类型 例如Guest operating ...

  4. JSP + servlet 源码 实现文件的上传

    JSP页面 upLoad.jsp _________________________________ <%@ page language="java" import=&quo ...

  5. 微信小程序数据分析之自定义分析

    在小程序后台,微信已经提供了强大的数据分析功能,包括实时统计.访问分析.来源分析和用户画像功能,可以说对一般的数据分析已经完全足够了,但有时应用需要做一些更加精准的数据分析,比如具体到某一个页面的分享 ...

  6. php的基础知识(四)

    14.数组: 索引数组: 下标就是数字开始的. $arr = ['a','b','c',1,2,3]; 关联数组: $arr = [ 'a' => 'b', 'c' => 'd'; 'e' ...

  7. Spark运行模式_spark自带cluster manager的standalone cluster模式(集群)

    这种运行模式和"Spark自带Cluster Manager的Standalone Client模式(集群)"还是有很大的区别的.使用如下命令执行应用程序(前提是已经启动了spar ...

  8. C语言实例解析精粹学习笔记——18

    <C语言实例解析精粹>中编译环境采用的是Turbo C 2.0.但是这个编译器年代久远,较新的编译器对书中的某些例子支持不好,在学习的时候同时做一些笔记. 实例18:将一个无符号整数转换为 ...

  9. HyperLedger Fabric 1.4 问题汇总(16)

    16.1 在运行e2e_cli例子时,执行./network_setup.sh up,出现错误:网络搭建之network e2ecli_default not found 问题原因: End-2-En ...

  10. BZOJ2330_糖果_KEY

    题目传送门 看题目可知这是一道差分约束的题目. 根据每种关系建边如下: 对于每种情况建边,然后跑一边SPFA.(最长路) 因为可能会有自环或环的情况,都不可能存在. 跑SPFA时记录入队次数,超过N弹 ...