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
分析:
这题有两种解法,用于解决不同情况的问题:
1. 如果array 长度和 k 非常接近, 我们可以从array的最左边或者最右边移除 array.length - k个值就可以了。
2. 如果array长度远远大于k,我们可以先找到最大的一个数并且比x小的那个数,然后再以那个数开始从左右扩展,直到取到k个数。
方法一:
class Solution {
public List<Integer> findClosestElements(int[] arr, int k, int x) {
int left = , right = arr.length - ;
int remaining = arr.length - k;
while(remaining >= ) {
if (Math.abs(x - arr[left]) > Math.abs(arr[right] - x)) {
left++;
} else {
right--;
}
remaining--;
}
List<Integer> list = new ArrayList<>();
for (int i = left; i <= right; i++) {
list.add(arr[i]);
}
return list;
}
}
方法二:
class Solution {
public List<Integer> findClosestElements(int[] arr, int k, int x) {
int closest = closest(arr, x);
int left = closest;
int right = closest;
while (k > ) {
if (Math.abs(x - getValue(arr, left - )) <= Math.abs(x - getValue(arr, right + ))) {
left--;
} else {
right++;
}
k--;
}
List<Integer> list = new ArrayList<>();
for (int i = left; i <= right; i++) {
list.add(arr[i]);
}
return list;
}
private int closest(int[] arr, int x) {
int left = , right = arr.length - ;
while (left <= right) {
int mid = left + (right - left) / ;
if (arr[mid] == x) {
return mid;
} else if (arr[mid] < x) {
left = mid + ;
} else {
right = mid - ;
}
}
if (right < ) return left;
if (left >= arr.length) return right;
return Math.abs(x - arr[left]) > Math.abs(x - arr[right]) ? right : left;
}
private long getValue(int[] arr, int idx) {
if (idx < ) return Integer.MIN_VALUE;
if (idx >= arr.length) return Integer.MAX_VALUE;
return arr[idx];
}
}
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 ...
- [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 - 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绝对距离最近的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]
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] 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] 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 ...
随机推荐
- 检测web界面不能访问后重启
检测并重启脚本:checkAndRestart.sh #!/bin/bash nowpath=$(cd ")";pwd) source $nowpath/omcparam.prop ...
- C# 之 static的用法详解
有的东西你天天在用,但未必就代表你真正了解它,正如我之前所了解的 static . 一.静态类 静态类与非静态类的重要区别在于静态类不能实例化,也就是说,不能使用 new 关键字创建静态类类型的变量. ...
- GXOI/GZOI2019题解
GXOI/GZOI2019题解 P5300 [GXOI/GZOI2019]与或和 一眼题.. 显然枚举每个二进制位,答案就变成了全1子矩阵数量. 这个xjb推推,单调栈一下就行了. #include& ...
- 在Web界面中实现Excel数据大量导入的处理方式
在早期Bootstrap框架介绍中,我的随笔<结合bootstrap fileinput插件和Bootstrap-table表格插件,实现文件上传.预览.提交的导入Excel数据操作流程> ...
- SpringCloud(3)服务消费者(Feign)
上一篇文章,讲述了如何通过 RestTemplate+Ribbon 去消费服务,这篇文章主要讲述如何通过Feign去消费服务. 1.Feign简介 Feign是一个声明式的伪Http客户端,它使得写H ...
- Linux下redis的安装及配置
1.去官网下载redis(redis.io) 2.将其解压到根目录下 3.进入解压的目录,然后编译源程序, 如果不是root账户登录的,命令前面需要加sudo make make install PR ...
- 安装 Power BI 报表服务器
开始之前 建议在安装 Power BI 报表服务器之前先查看安装 Power BI 报表服务器所要满足的硬件和软件要求. Power BI 报表服务器产品密钥 Power BI Premium 如果已 ...
- Virtual Box虚拟机安装Ubuntu16.04以及整理的一些基本操作
事先声明,参考自:https://www.cnblogs.com/wyt007/p/9856290.html 撰写此文,纯属是为了便利以后换电脑重装. 转载请注明地址:https://www.cnbl ...
- CodeForces 868F Yet Another Minimization Problem(决策单调性优化 + 分治)
题意 给定一个序列 \(\{a_1, a_2, \cdots, a_n\}\),要把它分成恰好 \(k\) 个连续子序列. 每个连续子序列的费用是其中相同元素的对数,求所有划分中的费用之和的最小值. ...
- 【JVM】类加载机制
原文:[深入Java虚拟机]之四:类加载机制 类从被加载到虚拟机内存中开始,到卸载出内存为止,它的整个生命周期包括:加载.验证.准备.解析.初始化.使用和卸载七个阶段.它们开始的顺序如下图所示: 类加 ...