Merge Sorted Array (Leetcode 88)
Problem Link: https://leetcode.com/problems/merge-sorted-array/
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
ptr3=m+n-1
ptr1=m-1
ptr2=n-1
# Start traversing from back
while(ptr1>=0 and ptr2>=0):
if(nums1[ptr1]>nums2[ptr2]):
nums1[ptr3]=nums1[ptr1]
ptr1=ptr1-1
ptr3=ptr3-1
else:
nums1[ptr3]=nums2[ptr2]
ptr2=ptr2-1
ptr3=ptr3-1
# Fill nums1 with left over element of nums2
while(ptr2>=0):
nums1[ptr3]=nums2[ptr2]
ptr2=ptr2-1
ptr3=ptr3-1
Last updated