# [[Leetcode]658. Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/)
- 本题难度: Hard/Medium
- Topic: Data Structure
# Description
# 我的代码
```python
import bisect
class Solution:
def findClosestElements(self, arr,k,x):
l = len(arr)
sorted(arr)
f = bisect.bisect_left(arr,x)
if (f == l) or (f>0 and x - arr[f-1] < arr[f] - x):
f = f-1
if f == l -1:
return arr[-k:]
head = f-k-1 if f-k-1>0 else 0
while(head+k-1<l-1 and arr[head+k] - x < x - arr[head]):
head+=1
return arr[head:head+k]
```
本文提供了一种解决LeetCode上658题“查找K个最接近的元素”的算法实现,通过使用二分查找和滑动窗口技术,有效地在已排序数组中找到指定数量的最接近目标值的元素。

4682

被折叠的 条评论
为什么被折叠?



