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 ...
随机推荐
- 关于Eclipse的版本、分支、衍生版本
Eclipse 简介: Eclipse的历史: Eclipse的发布版本: Eclipse分支: 关于不同分支版本的区别,点击链接: http://www.eclipse.org/downloads/ ...
- JSP 学习总结 03 核心组件 Servlet
1 Servlet 简绍 Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生 ...
- 几种c++字符串split 函数实现的比较
文中的字符串split函数功能是 从字符串中按照特定的分隔符进行分割,分割的结果保存到std::vector中. 1. strtok实现 std::vector<std::string> ...
- 用Eclipse中的git提交代码流程
有更新有提交 Commit到本地,pull,然后再push 提交 Commit到本地 或者直接commit and Push 更新 先对比然后pull或者右键项目直接pull 有冲突时 有冲突的时候优 ...
- svn 服务器部署
系统环境:CentOS 7.x安装方式:yum install (源码安装容易产生版本兼容的问题)安装软件:系统自动下载SVN软件 #检查是否安装了低版本的SVN[root@localhost /]# ...
- Flutter之SliverAppBar
new SliverAppBar( leading: GestureDetector( child: Icon(Icons.arrow_back), onTap: () => Navigator ...
- Python——socketserver编程(客户端/服务器)
一.socketserver是标准库中的高级模块,它的目标是简化很多多样板代码,是创建网络客户端和服务器所必须的代码.(事件驱动) 二.模块类 BaseServer :包含核心服务器功能和mix-in ...
- 【NLP】Conditional Language Models
Language Model estimates the probs that the sequences of words can be a sentence said by a human. Tr ...
- 线程池、进程池(concurrent.futures模块)和协程
一.线程池 1.concurrent.futures模块 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 Pro ...
- Go语言的通道(1)-无缓冲通道
前言: 上文中我们采用了[原子函数]已经[共享锁]两种方式分别对多个goroutine进行了同步,但是在go语言中提供了另一种更好的方式,那就是使用通道(Channel). 一.通道是什么? 其实无论 ...