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:

  1. The value k is positive and will always be smaller than the length of the sorted array.
  2. Length of the given array is positive and will not exceed 104
  3. 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个元素的更多相关文章

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

  2. 【LeetCode】658. Find K Closest Elements 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/find-k-c ...

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

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

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

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

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

  8. 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 ...

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

随机推荐

  1. maven创建项目,打包出可执行Jar

    官网参考 http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html 配置多种打包方式 这个例子也不错 https://bl ...

  2. ubuntu交换Caps 和 ESC

    https://askubuntu.com/questions/363346/how-to-permanently-switch-caps-lock-and-esc This will allow y ...

  3. css:关于position和float

    在CSS中,我们是通过定位属性position来进行定位的,具体它有如下几个属性值.常见的属性有如下几个: absolute  生成绝对定位的元素,相对于static定位以外的第一个父元素进行定位.元 ...

  4. html lesson one

    Review Congratulations on completing the first lesson of HTML & CSS! You are well on your way to ...

  5. C++复习:函数模板和类模板

    前言 C++提供了函数模板(function template).所谓函数模板,实际上是建立一个通用函数,其函数类型和形参类型不具体指定,用一个虚拟的类型来代表.这个通用函数就称为函数模板.凡是函数体 ...

  6. C语言复习:结构体

    结构体专题 01.结构体类型定义及结构体变量定义     char c1,char c2, char name[62]; int age     char name[62]; int age,char ...

  7. angular的启动原理

    当你用浏览器去访问index.html的时候,浏览器依次做了如下一些事情: 加载html,然后解析成DOM: 加载angular.js脚本:加载完成后自执行,生成全局angular对象,监听DOMCo ...

  8. java学习大方向

    总结Java程序员成长之路   转载  http://bbs.javazhijia.com/topic/1bb0733f80d94aedb50cc3b66d9792b6.html 我也搞了几年JAVA ...

  9. 在iOS上使用ffmpeg播放视频

    国外靠谱的有这几个:1.Mooncatventures group https://github.com/mooncatventures-group 2.KxMoviePlayer (use Open ...

  10. pandas 常用清洗数据(三)排序,去重

    1.排序 DataFrame 按照Index排序 Series.order()进行排序,而DataFrame则用sort或者sort_index或者sort_values 2.去重, dt = dt. ...