[二分]力扣找到k个最接近的元素(c++实现)

class Solution {
public:
    vector findClosestElements(vector& arr, int k, int x) {
        vector res;
        int l = 0,r=arr.size()-1;
        while(l=x) r=mid;
            else l=mid+1;
        }
        int i=l-1,j=r;
        while(res.size()= arr.size() || (i >= 0 && x - arr[i] <= arr[j] - x)){
                res.push_back(arr[i--]);
            }else {
                res.push_back(arr[j++]);
            }
        }
        sort(res.begin(),res.end());
        return res;
        
    }
};

你可能感兴趣的:(算法,leetcode,c++,算法)