Leetcode 658.找到K个最接近的元素
找到k个最接近的元素
给定一个排序好的数组,两个整数 k 和 x,从数组中找到最靠近 x(两数之差最小)的 k 个数。返回的结果必须要是按升序排好的。如果有两个数与 x 的差值一样,优先选择数值较小的那个数。
示例 1:
输入: [1,2,3,4,5], k=4, x=3
输出: [1,2,3,4]
示例 2:
输入: [1,2,3,4,5], k=4, x=-1
输出: [1,2,3,4]
说明:
- k 的值为正数,且总是小于给定排序数组的长度。
- 数组不为空,且长度不超过 104
- 数组里的每个元素与 x 的绝对值不超过 104
更新(2017/9/19):
这个参数 arr 已经被改变为一个整数数组(而不是整数列表)。 请重新加载代码定义以获取最新更改。
思路
Intuitively, we can sort the elements in list arr by their absolute difference values to the target x. Then the sublist of the first k elements is the result after sorting the elements by the natural order.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; class Solution {
public List<Integer> findClosestElements(int[] arr, int k, int x) {
List<Integer> list=new ArrayList<>();
for(int number:arr){
list.add(number);
}
Collections.sort(list,(a, b)->a==b?a-b:Math.abs(a-x)-Math.abs(b-x));
list=list.subList(0,k);
Collections.sort(list);
return list;
}
}

Leetcode 658.找到K个最接近的元素的更多相关文章
- Java实现 LeetCode 658 找到 K 个最接近的元素(暴力)
658. 找到 K 个最接近的元素 给定一个排序好的数组,两个整数 k 和 x,从数组中找到最靠近 x(两数之差最小)的 k 个数.返回的结果必须要是按升序排好的.如果有两个数与 x 的差值一样,优先 ...
- leetcode 658找到k个最接近的元素
class Solution { public: vector<int> findClosestElements(vector<int>& arr, int k, in ...
- 658.找到K个最接近的元素
2020-03-10 找到 K 个最接近的元素 给定一个排序好的数组,两个整数 k 和 x,从数组中找到最靠近 x(两数之 差最小)的 k 个数.返回的结果必须要是按升序排好的.如果有两个数与 x 的 ...
- [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 寻找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] 347. Top K Frequent Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [leetcode]692. Top K Frequent Words K个最常见单词
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...
随机推荐
- [Pytorch] pytorch笔记 <二>
pytorch笔记2 用到的关于plt的总结 plt.scatter scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, ...
- 我对XCode Objective
我对XCode Objective-c Cocoa的简单理解 Xcode Xcode说的通俗一点就是开发OS X 和 iOS 应用程序的. 如果我们想要认真点说 ,Xcode 是运行在操作系统Mac ...
- Responsive设计 (响应式设计)
一.什么是响应式设计 维基百科是这样对响应式作的描述:“Responsive设计简单的称为RWD,是精心提供各种设备都能浏览网页的一种设计方法,RWD能让你的网页在不同的设备中展现不同的设计风格.” ...
- MyBatis的优缺点以及特点
特点: mybatis是一种持久层框架,也属于ORM映射.前身是ibatis. 相比于hibernatehibernate为全自动化,配置文件书写之后不需要书写sql语句,但是欠缺灵活,很多时候需要优 ...
- js 金额小写转换为大写
<script> jQuery(document).ready(function () { //当金额文本框失去焦点时,自动将数字转化为大写填充到 大写的文本框中 $("#Amo ...
- cnn中的fp和bp推导
昨天下午在单位从新推导了一遍fp和bp. 在整个网络的训练中,最关键的就是计算残差. 最后一层残差很容易,那么前面每一层的残差怎么计算呢? 总体来说,有多少权重就需要多少残差项来进行权重更新.每个权重 ...
- LeetCode46. Permutations
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- SAP标准导出功能 - 删除默认选定格式
我们经常会使用SAP系统的标准功能导出ALV显示的数据,一般会选择电子表格. 选择电子表格之后,需要选择电子表格的具体格式. 选择格式之后点击确定,会弹出保存对话框. 如果在使用这个功能的时候,选择了 ...
- NFS文件系统存储服务部署
1 NFS介绍 1.1 什么是NFS? NFS是Network File System的缩写,中文名称是网络文件系统.它的主要功能是通过网络让不用的主机系统之间可以共享文件或者目录.NFS客户端通过挂 ...
- MySQL字段属性介绍
引言 这次Qi号分享MySQL字段属性简介.下面资料是Qi号搜集大量资料与个人理解的整理. MySQL提供了一组可以赋给表中各个列的数据类型,每个类型都强制数据满足为该数据类型预先确定的一组规则,例如 ...