当前位置:网站首页>【LeetCode】658.找到K个最接近的数
【LeetCode】658.找到K个最接近的数
2022-08-06 14:03:00 【酥酥~】
题目
给定一个 排序好 的数组 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
题解
对序列进行: 减x的绝对值的排序
然后取前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;
}
};
双指针
从两端向中间缩小
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);
}
};
二分查找+双指针
先找到等于x或者偏左的下标
然后左边扩散k位, 右边扩散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);
}
};
边栏推荐
猜你喜欢

自然语言处理的前世、今生和未来

redis使用

机器学习笔记(吴恩达老师)

burst!Ni Xingjun served as the chairman of Alipay China. He was born in technology and wrote the first line of "Alipay" code......

接口的安全设计三要素:ticket,签名,时间戳

eventfd和__thread的应用

shell实现加密压缩文件自动解压

梅科尔工作室OpenHarmony设备开发培训笔记-第5章学习笔记

Latex论文写作小技巧记录,不断更新

剖析SGI STL空间配置器(核心设计:_S_chunk_alloc函数)
随机推荐
Golang context.Context
This article will take you to understand the technical principles of CDN!
力扣练习——50 网络延迟时间
==和equals()的区别
微服务数据库分库设计解决方案(跨库关联查询、分布式事务处理)
Analysis of Rocket MQ Crash-Safe Mechanism
Go 限流的常见方法
Security on the sixth day practice after class
Total Software Deployment为您的企业网络管理软件部署
Validate date format
FinalShell remote connection operation
LeetCode刷题日记:1545. 找出第 N 个二进制字符串中的第 K 位
R language ggplot2 visualization: Visualize multi-category variable box plot (Box Plot), customize the fill color of box plot box, add main title, subtitle, caption information
Logstash、Filebeat安装与数据同步
剖析SGI STL空间配置器(deallocate内存回收函数和reallocate内存扩充函数)
mybaits-plus笔记
LeetCode high frequency question 78. Subset, return all possible subsets (power sets) of the array, generate all subsequences
R语言ggplot2可视化:可视化多分类变量箱图(Box Plot)、自定义箱图箱体的填充色、添加主标题、副标题、题注信息
mosquitto使用的基本流程以及一些遇见的问题
redis data types and common commands