최대 1 분 소요

class Solution(object):
    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
        tmp = nums1[:m]
        tmp += nums2[:n]
        tmp.sort()

Problem

I haven’t considered about ‘Time complexity’ and the constraint of ‘nums1.length == m + n’. Because, It is my first time to solve the Leet’s problem. From now on, I have to read througly…

class Solution(object):
    def merge(self, nums1, m, nums2, n):
        p1, p2 = m - 1, n - 1
        p = m + n - 1
        
        while p1 >= 0 and p2 >= 0:
            if nums1[p1] > nums2[p2]:
                nums1[p] = nums1[p1]
                p1 -= 1
            else:
                nums1[p] = nums2[p2]
                p2 -= 1
            p -= 1

        while p2 >= 0:
            nums1[p] = nums2[p2]
            p2 -= 1
            p -= 1

key

Using “two pointers”

IMG_CDF84CCE341C-1

about

        while p2 >= 0:
            nums1[p] = nums2[p2]
            p2 -= 1
            p -= 1

IMG_74D607A682FE-1

댓글남기기