当前位置:网站首页>【LeetCode】658. Find the K closest numbers
【LeetCode】658. Find the K closest numbers
2022-08-06 14:04:00 【Crispy~】
题目
给定一个 排序好 的数组 arr ,两个整数 k 和 x ,从数组中找到最靠近 x(两数之差最小)的 k 个数.返回的结果必须要是按升序排好的.
整数 a 比整数 b 更接近 x 需要满足:
|a - x| < |b - x| 或者
|a - x| == |b - x| 且 a < b
示例 1:
输入:arr = [1,2,3,4,5], k = 4, x = 3
输出:[1,2,3,4]
示例 2:
输入:arr = [1,2,3,4,5], k = 4, x = -1
输出:[1,2,3,4]
提示:
1 <= k <= arr.length
1 <= arr.length <= 104
arr 按 升序 排列
-104 <= arr[i], x <= 104
题解
对序列进行: 减xThe ordering of the absolute value of
然后取前k个
class Solution {
public:
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
if(arr.size() == k)
return arr;
sort(arr.begin(),arr.end(),[=](int &a,int &b){
return abs(a-x)==abs(b-x)?a<b:abs(a-x)<abs(b-x);});
vector<int> res(arr.begin(), arr.begin() + k);
sort(res.begin(),res.end());
return res;
}
};
双指针
Shrink from the ends to the middle
class Solution {
public:
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
int len = arr.size();
int left = 0;
int right = len-1;
while(right-left >= k)
{
int mid = left + (right-left)>1;
//cout<<abs(arr[left]-x) << " "<<abs(arr[right]-x)<<endl;
if( abs(arr[left]-x) > abs(arr[right]-x) )
left+=1;
else
right-=1;
}
//cout<<left<<" "<<right;
return vector(arr.begin()+left,arr.begin()+right+1);
}
};
二分查找+双指针
先找到等于xOr left subscript
Then spread to the leftk位, 右边扩散k位
然后进行压缩,压缩到k个
class Solution {
public:
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
int len = arr.size();
int left = 0;
int right = len-1;
while(left<right)
{
int mid = ((right-left+1) >> 1) + left;
cout<<left<<"=="<<mid<<"=="<<right<<endl;
if(arr[mid]>x)
right = mid-1;
else
left = mid;
}
right = min(len-1,left+k);
left = max(0,left-k);
while(right-left+1 > k)
{
if( abs(arr[left]-x) > abs(arr[right]-x) )
left+=1;
else
right-=1;
}
return vector(arr.begin()+left,arr.begin()+right+1);
}
};
边栏推荐
猜你喜欢

DC-9--vulnhub range

论文解读:《DeepKla:一种基于注意力机制的深度神经网络,用于蛋白质赖氨酸乳酸化位点预测》

LeetCode high frequency question 78. Subset, return all possible subsets (power sets) of the array, generate all subsequences

typeindex类型支持库学习

redis使用

IO流学习

MODBUS to PROFINET gateway to connect power intelligent monitoring instrument to PROFINET network case

七夕了,给你的那个TA画上一箭倾心吧~

TcpServer::start都做了些什么

burst!Ni Xingjun served as the chairman of Alipay China. He was born in technology and wrote the first line of "Alipay" code......
随机推荐
mosquitto使用的基本流程以及一些遇见的问题
1. What are microservices?
在数据中查找信号
ReentrantLock study notes
读《架构师修炼之道》有感
LeetCode high frequency question 78. Subset, return all possible subsets (power sets) of the array, generate all subsequences
【直播预告】对话知道创宇丨如何守住内容安全生命线?
【paper速读】NLNL: Negative Learning for Noisy Labels (ICCV2019)
重写muduo网络库开发环境和前期准备
LeetCode_递归_中等_397.整数替换
”元宇宙“必须具备这些特点
Swift如何更灵活的使用switch...case操作符
Talking about Tree Arrays
天梯赛真题——7-6 老板的作息表(25 分)
The difference between == and equals()
unity2D横版游戏教程10-场景控制
Rocket MQ Crash-Safe机制浅析
开源一夏 | 自从我使用HiFlow场景连接器后,在也不用担心成为“落汤鸡”了
Installation configuration of QT under Win10 (available for personal testing)
梅科尔工作室OpenHarmony设备开发培训笔记-第7章学习笔记