python - Merge Sorted Array in leetcode: The in-place modification of list doesn't work -


the link question: merge sorted array

i don't know why solution doesn't modify list nums1 when exiting function merge. here code:

def merge(self, nums1, m, nums2, n):     """     :type nums1: list[int]     :type m: int     :type nums2: list[int]     :type n: int     :rtype: void not return anything, modify nums1 in-place instead.     """     = 0     j = 0     while < m , j < n:         if nums1[i] < nums2[j]:             += 1         else:             nums1 = nums1[:i-1] + [nums2[j]] + nums1[i-1:]             += 1             j += 1     if == m:         nums1 = nums1 + nums2 

lists mutable, can create behavior looking for. need assign new values specific indices in nums1. when use splicing in fact creating new lists. use list functions [].insert(), [].pop(), [].extend() achieve functionality looking for.


Comments