658. 找到 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 已经被改变为一个整数数组(而不是整数列表)。 请重新加载代码定义以获取最新更改。

class Solution {
public List<Integer> findClosestElements(int[] arr, int k, int x) {
List<Integer> res = new ArrayList();
int start =0;
int end = arr.length-1;
while(end - start>=k){
if(x-arr[start] > arr[end]-x){
start++;
}else{
end--; }
}
for(int i=start;i<=end;i++){
res.add(arr[i]);
}
return res; }
}

Java实现 LeetCode 658 找到 K 个最接近的元素(暴力)的更多相关文章

  1. Leetcode 658.找到K个最接近的元素

    找到k个最接近的元素 给定一个排序好的数组,两个整数 k 和 x,从数组中找到最靠近 x(两数之差最小)的 k 个数.返回的结果必须要是按升序排好的.如果有两个数与 x 的差值一样,优先选择数值较小的 ...

  2. leetcode 658找到k个最接近的元素

    class Solution { public: vector<int> findClosestElements(vector<int>& arr, int k, in ...

  3. 658.找到K个最接近的元素

    2020-03-10 找到 K 个最接近的元素 给定一个排序好的数组,两个整数 k 和 x,从数组中找到最靠近 x(两数之 差最小)的 k 个数.返回的结果必须要是按升序排好的.如果有两个数与 x 的 ...

  4. [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 ...

  5. Java for LeetCode 023 Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...

  6. Java实现 LeetCode 60 第k个排列

    60. 第k个排列 给出集合 [1,2,3,-,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" &q ...

  7. [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 ...

  8. [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 ...

  9. Java实现 LeetCode 802 找到最终的安全状态 (DFS)

    802. 找到最终的安全状态 在有向图中, 我们从某个节点和每个转向处开始, 沿着图的有向边走. 如果我们到达的节点是终点 (即它没有连出的有向边), 我们停止. 现在, 如果我们最后能走到终点,那么 ...

随机推荐

  1. 软路由OpenWrt(LEDE)2020.4.4编译 UnPnP+NAS+多拨+网盘+DNS优化

    近期更新:2020.04.24编译-基于OpenWrt R2020.3.19版本.   2020.04.04更新记录: 修正国内域名加速脚本部分缺陷 内置打印机共享,ZeroTier 新增多套主题 S ...

  2. Azure AD(二)调用受Microsoft 标识平台保护的 ASP.NET Core Web API 上

    一,引言 上一节讲到Azure AD的一些基础概念,以及Azure AD究竟可以用来做什么?本节就接着讲如何在我们的项目中集成Azure AD 包含我们的API资源(其实这里还可以在 SPA单页面应用 ...

  3. 探索Linux内核:Kconfig / kbuild的秘密

    探索Linux内核:Kconfig / kbuild的秘密 文章目录 探索Linux内核:Kconfig / kbuild的秘密 深入了解Linux配置/构建系统的工作原理 Kconfig kbuil ...

  4. Java中Error和Exception的异同以及运行时异常(Runtime exception)与检查型异常(checked exception)的区别

    一:Error和Exception的基本概念: 首先Exception和Error都是继承于Throwable 类,在 Java 中只有 Throwable 类型的实例才可以被抛出(throw)或者捕 ...

  5. [whu1568]dp优化

    http://acm.whu.edu.cn/land/problem/detail?problem_id=1568 思路:先将所有数分解,得到2,3,5,7的个数,转化为用这些2,3,5,7" ...

  6. css中文字两端对齐兼容IE

    text-align: justify; text-justify:inter-ideograph;

  7. iptables做nat网络地址转换

    iptables做nat网络地址转换. 0. 权威文档 http://www.netfilter.org/documentation/HOWTO/NAT-HOWTO-6.html e文好的直接跳过本文 ...

  8. 【雕爷学编程】MicroPython动手做(03)——零基础学MaixPy之开机测试

    1.几个知识点(1)MicroPython 是 Python 3 语言的精简高效实现 ,包括Python标准库的一小部分,并针对嵌入式微控制器(单片机)和受限制的环境进行了优化,它是Python延伸出 ...

  9. POJ1948 Triangular Pastures

    POJ1948 Triangular Pastures #include <iostream> #include <cmath> using namespace std; ; ...

  10. 第四篇:NLP(Natural Language Processing)自然语言处理

    NLP自然语言处理: 百度AI的 NLP自然语言处理python语言--pythonSDK文档: https://ai.baidu.com/docs#/NLP-Python-SDK/top 第三方模块 ...