可以增加一个数组用来返回结果,在比较两个数组元素大小的过程里,将排好序的元素不断插入到新数组中。
也可以不需要增加数组,将两个数组先合并再排序。
https://blog.csdn.net/IT_job/article/details/80214756
这篇文章中的代码只需两行,相比较自己的代码,碾压感太强了。
class Solution:
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: void Do not return anything, modify nums1 in-place instead.
"""
nums1[m:m+n]=nums2[:n]
nums1.sort()