Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
leetcode: finished #658
Browse files Browse the repository at this point in the history
  • Loading branch information
xqm32 committed Feb 21, 2024
1 parent a2cc921 commit 8066379
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions content/leetcode/2024/2.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,44 @@ title: 2024.2
draft: false
---

# 2024.2.21

```python
#
# @lc app=leetcode.cn id=658 lang=python3
#
# [658] 找到 K 个最接近的元素
#

# @lc code=start
from typing import List


class Solution:
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
left, right = 0, len(arr) - 1
while left < right:
mid = left + (right - left) // 2
if arr[mid] < x:
left = mid + 1
else:
right = mid
left, right = left - 1, left
while right - left - 1 < k:
if left < 0:
right += 1
elif right >= len(arr):
left -= 1
elif x - arr[left] <= arr[right] - x:
left -= 1
else:
right += 1
return arr[left + 1 : right]


# @lc code=end
```

# 2024.2.20

```python
Expand Down

0 comments on commit 8066379

Please sign in to comment.