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 ...
随机推荐
- Linux内存管理 (11)page引用计数
专题:Linux内存管理专题 关键词:struct page._count._mapcount.PG_locked/PG_referenced/PG_active/PG_dirty等. Linux的内 ...
- maven压缩js css
maven压缩<plugin> <!-- YUI Compressor Maven压缩插件 --> <groupId>net.alchim31.maven</ ...
- go语言之行--golang操作redis、mysql大全
一.redis 简介 redis(REmote DIctionary Server)是一个由Salvatore Sanfilippo写key-value存储系统,它由C语言编写.遵守BSD协议.支持网 ...
- Python是如何实现生成器的原理
python中函数调用的实质原理: python解释器(即python.exe)其实是用C语言编写的, 在执行python代码时,实际上是在用一个叫做Pyeval_EvalFramEx(C语言的函数) ...
- Photoshop调出清晰的阴雨天气山水风景照
既然我们前期拍摄到了一张效果还不错的照片,那么下一步就是通过后期处理得到最终的影像. 在处理之前,我们一定要做到胸有成竹,而不是盲目调整. 也就是说在还没调整照片的时候,就要计划和预想到最终的照片应该 ...
- PS快速调出天蓝色清新外景
原片: 一.调整光比 曝光 黑白灰. 二.调整色温(新手可用白平衡工具.左上角第3个)调整饱和度(自然饱和度和蓝原色) 三.互补色的运用(高光偏黄 加的蓝色 暗部发蓝青色 加的橙黄色) 四.调整好照片 ...
- 【学习总结】Git学习-参考廖雪峰老师教程-总
公元2018-10-21 实验室台式机 win7 64位 参考教程: 廖雪峰Git教程 其他资料:Git-book 北大一只总结的笔记,最终整理的时候可以参考:Git笔记 评论区看到的另一个人,总结在 ...
- springboot2.0整合shiro出现ShiroDialect报错 找不到org/thymeleaf/processor/attr/AbstractTextChildModifierAttrPr
包版本过低,找最新包 https://mvnrepository.com/ <dependency> <groupId>com.github.theborakompanioni ...
- 三菱FX系列PLC教程
标 题 日 期 点击 第一章:可编程控制器概论 2014-11-04 1401 1-0 课程概述 2014-11-05 192237 1-1 PLC的定义功能与特点 2014-11-05 16 ...
- Django+Vue打造购物网站(五)
注册和登陆 drf的认证 http://www.django-rest-framework.org/api-guide/authentication/ settings.py文件的配置 INSTALL ...