class Solution {
public:
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
//查找,二分法找到那个数的lowerbound然后左右指针比较;O(logn+2k)
vector<int>::iterator p=lower_bound(arr.begin(),arr.end(),x);
if(p!=arr.begin() && *p != x) p--;
vector<int> res;
int len=arr.size();
int l=p-arr.begin(),r=p-arr.begin();
//cout<<l<<","<<r<<endl;
while(r-l<k-){
if(l==){
r++;continue;
}
if(r==len-){
l--;continue;
}
if(arr[r+]-x>=x-arr[l-])
l--;
else
r++;
}
for(int i=l;i<=r;i++)
res.push_back(arr[i]);
return res;
}
};

leetcode 658找到k个最接近的元素的更多相关文章

  1. Java实现 LeetCode 658 找到 K 个最接近的元素(暴力)

    658. 找到 K 个最接近的元素 给定一个排序好的数组,两个整数 k 和 x,从数组中找到最靠近 x(两数之差最小)的 k 个数.返回的结果必须要是按升序排好的.如果有两个数与 x 的差值一样,优先 ...

  2. Leetcode 658.找到K个最接近的元素

    找到k个最接近的元素 给定一个排序好的数组,两个整数 k 和 x,从数组中找到最靠近 x(两数之差最小)的 k 个数.返回的结果必须要是按升序排好的.如果有两个数与 x 的差值一样,优先选择数值较小的 ...

  3. 658.找到K个最接近的元素

    2020-03-10 找到 K 个最接近的元素 给定一个排序好的数组,两个整数 k 和 x,从数组中找到最靠近 x(两数之 差最小)的 k 个数.返回的结果必须要是按升序排好的.如果有两个数与 x 的 ...

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

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

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

  7. [LeetCode] 347. Top K Frequent Elements 前K个高频元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

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

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

  9. [leetcode]692. Top K Frequent Words K个最常见单词

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

随机推荐

  1. swoole如何在后台运行

    swoole如何在后台运行 nohup php server.php &

  2. nginx搭建及加固

    系统使用的是centos7 Nginx安装及配置 Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务 安装 我是用的环境是ce ...

  3. 去掉或修改lightinthebox网址与标题中Wholesale关键词

    includes\languages\english.php define('SEO_COMMON_KEYWORDS','Wholesale'); 将里面的Wholesale换成你想显示的词即可.

  4. redis事务(转载)

    原文地址:http://blog.csdn.net/hechurui/article/details/49508749 Redis事务 首先,Redis本身是单线程的. redis中的事务(trans ...

  5. Linux使用技巧汇总

    Debian是我日常使用的桌面系统,这里记录了我在使用Debian和其他Linux时所有的问题和解决办法,以及一些其他的心得体会. 向Debian致敬! 找回桌面系统关机按钮 在/etc/polkit ...

  6. [人物存档]【AI少女】【捏脸数据】写实系列

    点击下载:AISChaF_20191023202713797.zip 点击下载:AISChaF_20191023202713797.zip

  7. js 循环post

    var url_s=["h/a","h/b","h/c"]; function post_test(url,callback) { //请求 ...

  8. C# 在知道对象时编译json 而不调用json类

    StringBuilder sb = new StringBuilder();            sb.Append('[');                       foreach (va ...

  9. Eclipse中注释乱码解决办法

    问题描述:将别人的Java工程导入自己的工作空间之后,有时候会出现注释乱码问题. 这是由于IDE对汉字的编码方式不同造成的.比如,原来的开发人员使用的是UTF-8编码方式,而现在开发人员使用的IDE使 ...

  10. 【HDOJ5943】Kingdom of Obsession(数论)

    题意:给定n个人,n个座位,人的编号是[1,n],座位的编号是[s+1,s+n],编号为i的人能坐在编号为j的座位上的条件是j%i=0 问是否存在一组方案使得座位和人一一对应 n,s<=1e9 ...