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. 关于MySQL优化问题

    众所周知在数据量庞大的情况下普通的SQL语句已经满足不了我们的需要了,这个时候就需要DBA去进行数据库的优化,而我们作为一名开发人员不能对数据库进行优化这时该怎么办呢?答案是只能在SQL语句上面进行优 ...

  2. mariadb或者mysql忘记root密码

    windows======================net stop mysql #先停止mysql或者在服务管理里面停止 直接打开Windows的命令行(CMD)窗口(以管理员身份运行),输入 ...

  3. NPOI读取Excel遇到的坑

    NPOI是POI的.NET版本.POI是用Java写成的库,能帮助用户在没有安装Office环境下读取Office2003-2007文件.NPOI在.NET环境下使用,能读写Excel/Word文件. ...

  4. Java中枚举的相关应用

    package example6; import org.junit.Test;/*1.什么是枚举? * 需要在颐堤港范围内取值,这个值只能是这个范围内的一个 * 使用枚举关键字enum * 枚举里也 ...

  5. Python学习笔记五:字符串常用操作,字典,三级菜单实例

    字符串常用操作 7月19日,7月20日 ,7月22日,7月29日,8月29日,2月29日 首字母大写:a_str.capitalize() 统计字符串个数:a_str.count(“x”) 输出字符, ...

  6. 计算阶乘的和v2.0(4分)

    题目内容: 假设有这样一个三位数m,其百位.十位和个位数字分别是a.b.c,如果m= a!+b!+c!,则这个三位数就称为三位阶乘和数(约定0!=1).请编程计算并输出所有的三位阶乘和数. 函数原型: ...

  7. 简单复习一下ArrayList的扩容原理

    刚刚跟几个好朋友喝完小酒回家,简单大概复习一下ArrayList的扩容原理,由于头有点小晕,就只大概说一下扩容的原理哈: 首先ArrayList实现了List接口,继承了AbstractList,大家 ...

  8. Java设计模式(19)——行为模式之责任链模式(chain of responsibilitiy)

    一.概述 概念 UML简图 角色 抽象处理器:定义处理请求的接口 具体处理器:接收到请求后可以选择处理,也可以选择发给下家处理(持有下家的引用) 当然这里必须指出,实际中纯的责任链模式很难寻找,一般是 ...

  9. 20145209 2016-2017-2 《Java程序设计》第9周学习总结

    20145209 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC简介 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JDBC ...

  10. BZOJ1452_Count_KEY

    题目传送门 二维树状数组,对于每个颜色开一个树状数组,用容斥求解. code: #include <cstdio> using namespace std; int read() { ') ...