[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 ...
随机推荐
- java 使用jsoup处理html字符
依赖的jar <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artif ...
- English Pronunciation Analysis | Advanced English Conversation
English Pronunciation Analysis | Advanced English Conversation Share Tweet Share Tagged With: Ben Fr ...
- Linux IP和网关配置
操作环境 SuSE11/SuSE10 配置方法一<永久有效,重启不失效> 通过修改/etc/sysconfig/network/ifcfg-eth*文件直接配置,服务器重启不失效,建议使用 ...
- week06 09 NodeJS Server as a RPCclient - jayson
nodeserver端的rpcclient 来调用后端backendserver端定义的add等方法 2个server连通 Make NodeJs as a client - Npm jayson 用 ...
- vue如果是首页了 不让其后退
history.pushState(null, null, document.URL); //首页加载时候先置空 window.addEventListener('popstate', functio ...
- cdnbest的站点日志保存时间怎么设置
站点的保存时间是以节点全局里的保存时间为准 站点的日志保存大小还不能设置
- metasploit framework(四):生成payload
RHOST是限制来源IP,这里不用修改 generate生成payload 假设'\x00'是坏字符,生成payload时就会对'\x00'进行编码,排除坏字符. -b 去掉坏字符 -t 指定生成的格 ...
- JMeter学习(二十一)关联(转载)
转载自 http://www.cnblogs.com/yangxia-test 话说LoadRunner有的一些功能,比如:参数化.检查点.集合点.关联,Jmeter也都有这些功能,只是功能可能稍弱一 ...
- 【Django】django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient?
今天创建APP的时候报这个错误django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you in ...
- C# sqlserver ExecuteNonQuery()方法详解
关于ExecuteNonQuery() 方法以前对这个一直都没在意,基本上都没有用其返回值,查了一下MSDN,如下:SqlCommand.ExecuteNonQuery 方法对连接执行 Transac ...