Leetcode 面试题 10.01. 合并排序的数组 (每日一题 20210616)
生活随笔
收集整理的這篇文章主要介紹了
Leetcode 面试题 10.01. 合并排序的数组 (每日一题 20210616)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給定兩個排序后的數組 A 和 B,其中 A 的末端有足夠的緩沖空間容納 B。 編寫一個方法,將 B 合并入 A 并排序。初始化?A 和 B 的元素數量分別為?m 和 n。示例:輸入:
A = [1,2,3,0,0,0], m = 3
B = [2,5,6], n = 3輸出:?[1,2,2,3,5,6]
說明:A.length == n + m鏈接:https://leetcode-cn.com/problems/sorted-merge-lcciclass Solution:def merge(self, A: List[int], m: int, B: List[int], n: int) -> None:"""Do not return anything, modify A in-place instead."""index_1, index_2, index_all = m - 1, n - 1, m + n -1while index_1 >= 0 and index_2 >= 0:if A[index_1] < B[index_2]:A[index_all] = B[index_2]index_all -= 1index_2 -= 1else:A[index_all] = A[index_1]index_all -= 1index_1 -= 1if index_2 >= 0:A[0:index_2+1] = B[0:index_2+1]return A
?
總結
以上是生活随笔為你收集整理的Leetcode 面试题 10.01. 合并排序的数组 (每日一题 20210616)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode(20210419-20
- 下一篇: Leetcode 剑指 Offer 03