(双指针、二分Binary Search) leetcode 658. Find K closest Elements

题意:给定一个升序排列的数组,找到k个与x最相近的元素(即差值最小),返回的结果必须要是按升序排好的。如果有两个数与 x的差值一样,优先选择数值较小的那个数。

解法一:双指针(排除法),一个一个删,因为是有序数组,且返回的是连续升序子数组,所以每一次删除的元素一定是位于边界;如果数组含有共 7 个元素,要保留 3 个元素,因此要删除 4 个元素(arr.size()-k);因为要删除的元素都位于边界,于是可以使用双指针(左指针指向数组的第一个元素,右指针指向数组最后一个元素)对撞的方式确定保留区间。

将需要删除的元素个数作为循环的条件,当right指向的元素减去x 大于或等于 left指向的元素减去x 的距离时,说明left指向的元素更接近x,故将right-1。(因为如果有两个数与 x的差值一样,优先选择数值较小的那个数)所以等于也是将right-1; 否则 left+1。

class Solution {
public:
    vector<int> findClosestElements(vector<int>& arr, int k, int x) {
        int size = arr.size(), n = size-k;
        int left = 0, right = size-1;
        while(n){

            if(x - arr[left] <= arr[right] - x)
                right--;
            else
                left++;
            n--;
        }
        vector<int> a;
//        for(int i=left; i<=right; ++i)
//            a.push_back(arr[i]);
        a.assign(arr.begin()+left, arr.begin()+right+1);
        return a;
    }
};

解法二: Binary Search

这道题的Binary Search 真的不好想 :(   下面是我整理的解题思路,参考了很多大神的思路(链接如下),突然发现二分搜索有好多变种啊,搞得我有点晕晕的。

我用的两个二分搜索模板:https://www.acwing.com/blog/content/31/

对应的讲解视频:https://www.bilibili.com/video/av41422769

二分搜索github的总结贴:https://github.com/yuzhoujr/leetcode/issues/8

二分搜索的总结博客:http://blackblog.tech/2018/10/08/LeetCode2Divide/#more

youtube上讲解这道题用二分搜索的视频:https://www.youtube.com/watch?v=3ifFNvdfjyg

class Solution {
public:
    vector<int> findClosestElements(vector<int>& arr, int k, int x) {
        int left = 0, right = arr.size()-k;
        while(left< right){
            int mid = left + (right-left)/2;
            if(x> arr[mid]){
                if(x-arr[mid] > arr[mid+k]-x)
                    left = mid+1;
                else
                    right = mid;
            }
            else

                right = mid;
        }
        return vector<int>(arr.begin()+left, arr.begin()+left+k);
    }
};

原文地址:https://www.cnblogs.com/Bella2017/p/11223088.html

时间: 2024-07-31 11:26:18

(双指针、二分Binary Search) leetcode 658. Find K closest Elements的相关文章

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 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 Outp

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 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 Outp

LeetCode Find K Closest Elements

原题链接在这里:https://leetcode.com/problems/find-k-closest-elements/description/ 题目: 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 smal

[LeetCode][Python]Top K Frequent Elements

op K Frequent Elements Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time

[Binary Search] Leetcode 35, 74

35. Search Insert Position Description Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Example 1: In

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 数组里的每个元

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

class Solution { public: vector<int> findClosestElements(vector<int>& arr, int k, int x) { //查找,二分法找到那个数的lowerbound然后左右指针比较:O(logn+2k) vector<int>::iterator p=lower_bound(arr.begin(),arr.end(),x); if(p!=arr.begin() && *p != x

LeetCode 347. Top K Frequent Elements

https://leetcode.com/problems/top-k-frequent-elements/ Lambda http://baike.baidu.com/link?url=ZK0qILx8cb_8HUX13JvVUYdnlJBOvAPJdWF83wD-tDgQQwIQYAVzkhytf_3f7oidHLPeTyXSswQUWVW51W42Ri4Pp8vnnkFHBCloSSTyS9xIT2pAV8a2zknUKK1UkjhDMzr4O7-qrD6J-lkJxET4u_#4 C++

[LeetCode] 347. Top K Frequent Elements 解题思路 - Java

Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must be bet