leetcode 658找到k个最接近的元素

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个最接近的元素的更多相关文章
- Java实现 LeetCode 658 找到 K 个最接近的元素(暴力)
658. 找到 K 个最接近的元素 给定一个排序好的数组,两个整数 k 和 x,从数组中找到最靠近 x(两数之差最小)的 k 个数.返回的结果必须要是按升序排好的.如果有两个数与 x 的差值一样,优先 ...
- Leetcode 658.找到K个最接近的元素
找到k个最接近的元素 给定一个排序好的数组,两个整数 k 和 x,从数组中找到最靠近 x(两数之差最小)的 k 个数.返回的结果必须要是按升序排好的.如果有两个数与 x 的差值一样,优先选择数值较小的 ...
- 658.找到K个最接近的元素
2020-03-10 找到 K 个最接近的元素 给定一个排序好的数组,两个整数 k 和 x,从数组中找到最靠近 x(两数之 差最小)的 k 个数.返回的结果必须要是按升序排好的.如果有两个数与 x 的 ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] 347. Top K Frequent Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [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 ...
随机推荐
- Caffe常用算子GPU和CPU对比
通过整理LeNet.AlexNet.VGG16.googLeNet.ResNet.MLP统计出的常用算子(不包括ReLU),表格是对比. Prelu Cpu版 Gpu版 for (int i = 0; ...
- ELF文件格式理解
ELF(Executable and Linking Format)是一种对象文件的格式,用于定义不同类型的对象文件(Object files)中都放了什么东西.以及都以什么样的格式去放这些东西.它自 ...
- C语言字符串函数总结
原文链接 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #include <s ...
- SpringMVC基础02——HelloWorld
1.搭建环境 博主使用的环境是IDEA2017.3,首先我们需要创建一个maven项目父项目,创建一个project,选择maven,之后点击next 添写当前项目的坐标,之后点击next 填写项目名 ...
- ssh无密码访问设置(ssh-keygen 的详解)
[原文链接]http://blog.csdn.net/wh_19910525/article/details/7433164 为了让两个linux机器之间使用ssh不需要用户名和密码.所以采用了数字签 ...
- vue-cli 4 安装与 新建项目 路由
环境: windows: vue-cli: 编辑器: vsCode npm: #去nodejs网安装https://npm.taobao.org/mirrors/node/v12.12.0/node- ...
- 02-springmvc分布式项目dataService项目配置
spring总文件 文件名:applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> ...
- 【洛谷P4309】最长上升子序列
题目大意:给定一个序列,初始为空.现在我们将 1 到 N 的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? 题解:学会了 rope 操 ...
- Redis 管道pipeline
Redis是一个cs模式的tcp server,使用和http类似的请求响应协议. 一个client可以通过一个socket连接发起多个请求命令. 每个请求命令发出后client通常会阻塞并等待red ...
- BeanUtils对象属性copy的性能对比以及源码分析
1. 对象属性拷贝的常见方式及其性能 在日常编码中,经常会遇到DO.DTO对象之间的转换,如果对象本身的属性比较少的时候,那么我们采用硬编码手工setter也还ok,但如果对象的属性比较多的情况下,手 ...