题目:原题链接(简单)

标签:排序、数组、双指针

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( N ) O(N) O(N) O ( 1 ) O(1) O(1) 48ms (20.19%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:
    def merge(self, A: List[int], m: int, B: List[int], n: int) -> None:
        i, i1, i2 = m + n - 1, m - 1, n - 1
        while True:
            if i1 >= 0 and i2 >= 0:
                if A[i1] >= B[i2]:
                    A[i] = A[i1]
                    i -= 1
                    i1 -= 1
                else:
                    A[i] = B[i2]
                    i -= 1
                    i2 -= 1
            elif i1 >= 0:
                A[i] = A[i1]
                i -= 1
                i1 -= 1
            elif i2 >= 0:
                A[i] = B[i2]
                i -= 1
                i2 -= 1
            else:
                break