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. Linux Exploit系列之一 典型的基于堆栈的缓冲区溢出

    Linux (x86) Exploit 开发系列教程之一(典型的基于堆栈的缓冲区溢出) Note:本文大部分来自于看雪hackyzh的中文翻译,加入了一些自己的理解 典型的基于堆栈的缓冲区溢出 虚拟机 ...

  2. Nginx如何配置https证书?

    #把80端口请求跳转到443端口 server { listen 80; server_name 域名; return 301 https://$http_host$request_uri; } se ...

  3. 94. Binary Tree Inorder Traversal (Java)

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

  4. deep_learning_Function_numpy_argmax()函数

    numpy里面的argmax函数 函数原型:def argmax(a, axis=None, out=None)a----输入arrayaxis----为0代表列方向,为1代表行方向out----结果 ...

  5. Zabbix Agent配置文件详解

    # This is a config file for the Zabbix agent daemon (Unix)# To get more information about Zabbix, vi ...

  6. 关于LRU算法(转载)

      原文地址: http://flychao88.iteye.com/blog/1977653 http://blog.csdn.net/cjfeii/article/details/47259519 ...

  7. Gym - 101908H Police Hypothesis (树链剖分/LCT+字符串哈希)

    题意:有一棵树,树上每个结点上有一个字母,有两种操作: 1)询问树上两点u,v间有向路径上有多少个字母和某个固定的字符串相匹配 2)将结点u的字母修改为x 树剖+线段,暴力维护前缀和后缀哈希值(正反都 ...

  8. 使用CEfSharp之旅 前后端访问代码

    1.引入CEfSharp newget包 2.把平台配置为X86或X64,any cpu不支持此控件 3.引入命名空间 using CefSharp; using CefSharp.WinForms; ...

  9. 未能从程序集“netstandard, Version=2.0.0.0......”中加载类型“...”。

      需要在机器上安装:4.7.1的framework 下载地址: https://www.microsoft.com/zh-CN/download/details.aspx?id=56116 说明:n ...

  10. BZOJ 3173: [Tjoi2013]最长上升子序列 Splay

    一眼切~ 重点是按照 $1$~$n$ 的顺序插入每一个数,这样的话就简单了. #include <cstdio> #include <algorithm> #define N ...