Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from labuladong:master #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
jenningsloy318 merged 1 commit into AlgorithmAndLeetCode:master from labuladong:master
Mar 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions 多语言解法代码/solution_code.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -24474,26 +24474,28 @@ class PriorityQueue {
import heapq

class Solution:
def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:
def kSmallestPairs(self, nums1, nums2, k):
# 存储三元组 (num1[i], nums2[i], i)
# i 记录 nums2 元素的索引位置,用于生成下一个节点
pq = []
pq = []
for i in range(len(nums1)):
heapq.heappush(pq, [nums1[i], nums2[0], 0])
heapq.heappush(pq, (nums1[i] + nums2[0], nums2[0], 0))

res = []
# 执行合并多个有序链表的逻辑
while pq and k > 0:
cur = heapq.heappop(pq)
k -= 1
# 链表中的下一个节点加入优先级队列
next_index = cur[2] + 1
# 获取第一个链表节点
node = cur[0] - cur[1]
if next_index < len(nums2):
heapq.heappush(pq, [cur[0], nums2[next_index], next_index])
pair = [cur[0], cur[1]]
heapq.heappush(pq, (node + nums2[next_index], nums2[next_index], next_index))

pair = [node, cur[1]]
res.append(pair)

return res
```

Expand Down Expand Up @@ -71073,4 +71075,4 @@ class Solution:
return s[n:] + s[:n]
```

https://leetcode.cn/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof 的多语言解法👆
https://leetcode.cn/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof 的多语言解法👆

AltStyle によって変換されたページ (->オリジナル) /