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 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
UPDATE (2017/9/19):
The arr parameter had been changed to an array of integers (instead of a list of integers). Please reload the code definition to get the latest changes.
中了 priorityqueue 的毒,自己写了个比较繁琐的方法:
class Solution {
public List<Integer> findClosestElements(int[] arr, int k, int x) {
if(arr == null || arr.length == 0){
return new ArrayList<Integer>();
}
PriorityQueue<int[]> pq = new PriorityQueue<>((e1,e2) -> elementCompare(e1,e2));
for(int a : arr){
pq.add(new int[]{a,Math.abs(a-x)});
if(pq.size() > k){
pq.poll();
}
}
List<Integer> list = new ArrayList<>();
while(!pq.isEmpty()){
list.add(pq.poll()[0]);
}
Collections.sort(list);
return list;
}
private int elementCompare(int[] e1, int[]e2){
if(e1[1] != e2[1]){
return e2[1] - e1[1];
}
else{
return e2[0] - e1[0];
}
}
}
更合适的方法:
用binary search + 双指针来做, 注意最后加入list中的顺序问题:
class Solution {
public List<Integer> findClosestElements(int[] arr, int k, int x) {
if(arr == null || arr.length == 0){
return new ArrayList<Integer>();
}
List<Integer> list = new ArrayList<>();
int len = arr.length;
if(x >= arr[len-1]){
for(int i = len - k; i<len; i++){
list.add(arr[i]);
}
}
else if(x <= arr[0]){
for(int i = 0; i<k; i++){
list.add(arr[i]);
}
}
else{
int n = 0;
int l = 0;
int h = len - 1;
while(l <= h){
int mid = l + (h-l)/2;
if(arr[mid] == x || (arr[mid] > x && arr[mid-1] < x)){
n = mid;
break;
}
else if(arr[mid] > x){ h = mid - 1;
}
else if(arr[mid] < x){
l = mid + 1;
} }
int less = n-1, more = n;
while(less >= 0 && more < len && k > 0){
if(Math.abs(arr[less] - x) <= Math.abs(arr[more] - x)){
list.add(0, arr[less]);
less -- ;
}
else{
list.add(arr[more]);
more ++;
}
k--;
}
while(less >= 0 && k > 0){
list.add(0, arr[less--]);
k--;
}
while(more < len && k > 0 ){
list.add(arr[more++]);
k--;
}
}
return list;
} }
LeetCode - Find K Closest Elements的更多相关文章
- [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] 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]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 ...
- [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 ...
- 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-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 ...
- 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 ...
- [LeetCode] 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 ...
随机推荐
- U启动安装原版Win7系统教程
1.制作u启动u盘启动盘2.下载原版win7系统镜像并存入u盘启动盘3.硬盘模式更改为ahci模式 第一步: 将准备好的u启动u盘启动盘插在电脑usb接口上,然后重启电脑,在出现开机画面时通过u盘启动 ...
- day25-python操作redis一
1. Python操作nosql数据库 NoSQL,泛指非关系型的数据库.随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的web2 ...
- xshell提示必须安装最新的更新
今天大家的xshell基本都出了这个问题 调整时间,调整到比较前的时间,打开xshell即可. 然后工具->选项 把更新去了
- day18 类与类之间的关系
今日所学内容: 1.类与类之间的关系 2.self 到底是谁? 3. 特殊成员 : 1.类与类之间的关系 在我们的世界中事物和事物之前总会有一些联系 在面向对象中,类与类之间也可以产生相关的联系 1) ...
- HTML语义化简介思维导图
- Linux音频驱动学习之:(2)移植wm8976声卡驱动(linux-3.4.2)
1.wm8976驱动程序: /* * wm8976.h -- WM8976 Soc Audio driver * * This program is free software; you can re ...
- spring boot 发邮件
报错: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not con ...
- SUSE_LINUX 11 SP3 安装 IBM MQ 7.5
0.环境介绍 mq7.5 suse linux 11 1. 上传安装包 上传安装包到 softWare/CI79IML.tar.gz 2. 安装证书 sh ./mqlicense.sh 输入 1 同意 ...
- pytest的HTML
安装html: 1.github上源码地址[https://github.com/pytest-dev/pytest-html] 2.pip安装 pip install pytest-html 3. ...
- Qt 查询字符串数据
(1)函数QString::startsWith(),判断某一个字符串是否以某个字符串开头:该函数具有两个参数,第一个参数制定了一个字符串,第二个参数指定是否大小写敏感,默认大小写敏感: eg: QS ...