[Python] merge sorted array wrong result not sure why? [duplicate]

Discussão em 'Python' iniciado por Stack, Julho 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am doing this challenge but I am getting two different results one on my pycharm and one on the leet code webpage.

    my code on pycharm:

    nums1 = [1,2,3,0,0,0]
    m = 3
    nums2 = [2,5,6]
    n = 3
    print(nums1[0:m])
    print(nums2[0:n])
    nums1 = nums1[0:m] + nums2[0:n]


    nums1.sort()
    print(nums1)


    and the result:

    [1, 2, 3]
    [2, 5, 6]
    [1, 2, 2, 3, 5, 6]


    my leet code:

    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.
    """
    nums1=nums1[0:m]+nums2[0:n]
    nums1.sort()


    the leet code result:

    Input
    nums1 = [1,2,3,0,0,0]
    m = 3
    nums2 = [2,5,6]
    n = 3
    Output
    [1,2,3,0,0,0]
    Expected
    [1,2,2,3,5,6]


    Both are python 3.

    Continue reading...

Compartilhe esta Página