leetcode 658. Find K Closest Elements 寻找绝对距离最近K个元素+ 双指针遍历
2017-12-21 18:01
447 查看
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
UPDATE (2017/9/19):
The arr parameter had been changed to an array of integers (instead of a list of integers). Please reload the code definition to get the latest changes.
本题题意很简单,最直接的方法,就是暴力查找,但是后来发现一个更加方便的方法,很棒的做法。
这道题给我们了一个数组,还有两个变量k和x。让我们找数组中离x最近的k个元素,而且说明了数组是有序的,如果两个数字距离x相等的话,取较小的那个。从给定的例子可以分析出x不一定是数组中的数字,我们想,由于数组是有序的,所以最后返回的k个元素也一定是有序的,那么其实就是返回了原数组的一个长度为k的子数组,转化一下,实际上相当于在长度为n的数组中去掉n-k个数字,而且去掉的顺序肯定是从两头开始去,应为距离x最远的数字肯定在首尾出现。那么问题就变的明朗了,我们每次比较首尾两个数字跟x的距离,将距离大的那个数字删除,直到剩余的数组长度为k为止,
代码如下:
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
UPDATE (2017/9/19):
The arr parameter had been changed to an array of integers (instead of a list of integers). Please reload the code definition to get the latest changes.
本题题意很简单,最直接的方法,就是暴力查找,但是后来发现一个更加方便的方法,很棒的做法。
这道题给我们了一个数组,还有两个变量k和x。让我们找数组中离x最近的k个元素,而且说明了数组是有序的,如果两个数字距离x相等的话,取较小的那个。从给定的例子可以分析出x不一定是数组中的数字,我们想,由于数组是有序的,所以最后返回的k个元素也一定是有序的,那么其实就是返回了原数组的一个长度为k的子数组,转化一下,实际上相当于在长度为n的数组中去掉n-k个数字,而且去掉的顺序肯定是从两头开始去,应为距离x最远的数字肯定在首尾出现。那么问题就变的明朗了,我们每次比较首尾两个数字跟x的距离,将距离大的那个数字删除,直到剩余的数组长度为k为止,
代码如下:
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <string> #include <climits> #include <algorithm> #include <sstream> #include <functional> #include <bitset> #include <numeric> #include <cmath> #include <regex> using namespace std; class Solution { public: vector<int> findClosestElements(vector<int>& arr, int k, int x) { vector<int> res = arr; while (res.size() > k) { int left = abs(res[0]-x); int right = abs(res.back() - x); if (left > right) res.erase(res.begin()); else res.pop_back(); } return res; } };
相关文章推荐
- [LeetCode] Find K Closest Elements 寻找K个最近元素
- [LeetCode] 658. Find K Closest Elements 寻找K个最近元素
- leetcode 542. 01 Matrix 距离0最近的距离 + 正反遍历 + 动态规划DP
- 已排序的数组中找到k个距离x最近的元素
- leetcode 61. Rotate List 向右旋转k个元素 + 链表环遍历
- 元素到页面的绝对距离
- JQ 遍历节点 取得元素集合 前面同辈 后面同辈 前后所有同辈 最近匹配
- 寻找指定元素的指针
- 遍历m个元素取k个的递归解法
- leetcode 448. Find All Numbers Disappeared in an Array 寻找缺失元素+很棒O(n)做法
- 2.11寻找最近的点对 (给定一系列的点,求出距离最短的点对)
- leetcode 236. Lowest Common Ancestor of a Binary Tree 最近公告祖先LCA + 二叉树 + 深度优先遍历DFS
- position的值, relative和absolute分别是相对于谁进行定位的? absolute :生成绝对定位的元素, 相对于最近一级的 定位不是 static 的父元素来进行定位。 fi
- 寻找指定元素的指针
- LeetCode-162. Find Peak Element (JAVA)寻找peak元素
- 寻找最小的k个数(先快速排序,然后输出前k个元素)
- leetcode596:496. Next Greater Element I(寻找元素对应位置的下一个较大值)
- 寻找N个元素中最大的K个元素解法
- leetcode之层次遍历,让每个节点的next指针指向右边
- leetcode_436. Find Right Interval 找距离最近的右间隔,对结构体对象的列表排序