[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 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:
- The value k is positive and will always be smaller than the length of the sorted array.
- Length of the given array is positive and will not exceed 104
- Absolute value of elements in the array and x will not exceed 104
题目
思路
本题要求我们在sorted array中找出K个最相近于x的数。因为输出结果一定排好序的、k-size的区间,若能找出该区间的leftBound,向右边走k个值,就可以得到desired output。
即问题转化为,怎样找到该leftBound呢? 在[0, n-k]中使用binary search查找



代码
class Solution {
public List<Integer> findClosestElements(int[] arr, int k, int x) {
int begin = 0;
int end = arr.length - k;
while(begin < end){
int mid = begin + (end - begin) /2 ;
if(x > arr[mid]){
if( x - arr[mid] > arr[mid + k] - x){
begin = mid +1;
}else{
end = mid;
}
}else{
end = mid;
}
}
int index = begin;
List<Integer> result = new ArrayList<>() ;
while( k != 0){
result.add(arr[index++]);
k--;
}
return result;
}
}
[leetcode]658. Find K Closest Elements绝对距离最近的K个元素的更多相关文章
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/find-k-c ...
- [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 ...
- 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 ...
- 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 ...
- [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]
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
- 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 ...
- [leetcode]347. Top K Frequent Elements 最高频的前K个元素
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
随机推荐
- TP框架(接口文档模板框架)
https://blog.csdn.net/s390124971/article/details/51995497
- SSL 链接安全协议的enum
摘自:https://blog.csdn.net/lan_liang/article/details/70948221 在进行HTTPS连接时,需要指定SecurityProtocol.对于.NET ...
- jquery接触初级-----juqery选择器实例
jquery 选择器用于触发事件,可以少写很多js代码,一般来说,基本的特效功能都能够完成 这里列举一个简单的jquery写的例子: 要求:有两种情况: 1.产品最初状态显示简约形式的品牌,即显示部分 ...
- 4.Python文件操作
文件内需要写入的内容 Seems the love I’ve ever known 看来,过去我所知道的爱情 Has always been the most destructive kind 似乎总 ...
- Java IO中转换流的作用
在<Java网络编程>中,有这样一段话: ”Reader和Writer最重要的子类是InputStreamReader和OutputStreamWriter类. InputStreamRe ...
- 遍历DOM树,链式操作
如果需要在同一个选取结果上使用多个jQuery方法,可以同时列出这些方法,并用.隔开,如下面的代码. 1 $("#one").hide().delay(500).fadeIn(15 ...
- RPN(region proposal network)之理解
在faster-r-cnn 中,因为引入rpn层,使得算法速度变快了不少,其实rpn主要作用预测的是 “相对的平移,缩放尺度”,rpn提取出的proposals通常要和anchor box进行拟合回归 ...
- 历届试题 小数第n位-(同余公式+快速幂)
问题描述 我们知道,整数做除法时,有时得到有限小数,有时得到无限循环小数. 如果我们把有限小数的末尾加上无限多个0,它们就有了统一的形式. 本题的任务是:在上面的约定下,求整数除法小数点后的第n位开始 ...
- Winform 事件
事件参数:object sender - 事件主体EventArgs e - 事件数据函数体 - 我进行的操作 常用事件:点击事件click (1)Load事件:该事件在窗体加载到内存时发生,即在第一 ...
- gradle 很好用的么
Gradle 其实是很好用的 2017, Apr 14 by Tesla Ice Zhang Gradle 是一款使用 Kotlin (划掉) Groovy 编写的 JVM 构建工具,其易用性和 Ma ...