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. 90. Subsets II (Java)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  2. 关于IDEA的application.properties读取乱码,以及显示乱码问题

    设置编码 如果设置之后还是不成功,就重启IDEA 再不行就删除application.properties重新编辑, 我采用的是注释掉要读取的中文部分,再下面再写一行

  3. 英国电信选择由 Canonical 开发的 Ubuntu OpenStack 作为云平台

    英国电信(简称 BT,British Telecom)宣布,选择由 Canonical 开发的 Ubuntu OpenStack 作为云平台,该平台将有助于支持引入 5G 和光纤到户的连接. 作为 U ...

  4. win10 安装ubuntu16.04双系统

    安装了两天的ubuntu系统,很是头疼,发现网上的内容,比较繁杂,因此,写此博客,进行综合整理,总结了安装方法.方便大家安装,减少搜索. 电脑是老师的电脑,配置为: 主板:微星 CPU:英特尔i5 7 ...

  5. Linux: df du

    df :列出文件系统的整体磁盘使用量 du :评估文件系统的磁盘使用量(常用在推估目录所占容量) 1 df  :[-ahikHTm] 目录或文件名 选项或参数: -a:  列出所有的文件系统,包括系统 ...

  6. ansible简要说明

    说明 Ansible是一个python编写模型驱动的配置管理器,支持多节点发布.远程任务执行.默认使用 SSH 进行远程连接.无需在被管理节点上安装附加软件,可使用各种编程语言进行扩展.本文基于ans ...

  7. SpringBoot 出现内置的Tomcat没有启动的情况

    Tomcat未启动,也未报错 pom.xml文件中增加 <dependency> <groupId>org.springframework.cloud</groupId& ...

  8. Python CGI编程Ⅲ

    GET和POST方法 浏览器客户端通过两种方法向服务器传递https://www.xuanhe.net/信息,这两种方法就是 GET 方法和 POST 方法. 使用GET方法传输数据 GET方法发送编 ...

  9. 对abel 转译 class 过程的研究----------------------引用

    作为当下最流行的 JavaScript 编译器,Babel 替我们转译 ECMAScript 语法,而我们不用再担心如何进行向后兼容. 零.前言 虽然在 JavaScript 中对象无处不在,但这门语 ...

  10. itertools模块、排列、组合、算法

    关于列表重组的python小题 题目一:给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集).             说明:解集不能包含重复的子集. 示例:输入: nums = ...