LeetCode: Arrays 101: Inserting Items Into an Array


Here are my solutions for the second part of the card: Arrays 101, from LeetCode.

Duplicate Zeroes

Given an array of integers, remove duplicate zeroes and shift the remaining elements.


class Solution:
    def duplicateZeros(self, arr: List[int]) -> None:
        Do not return anything, modify arr in-place instead.
        index = 0
        arr_length = len(arr)
        while index < arr_length:
            if arr[index] == 0:
                arr.insert(index, 0)
                index += 1
            index += 1

 Merge Sorted Array

Given two sorted arrays, merge them together into nums1.


This problem wasn’t very Pythonic and I actually want to practice my problem solving skills together with my Python skills, so, I’ve took the liberty to modify the problem a little:

from typing import List

class Solution:
    def merge(self, nums1: List[int], nums2: List[int]) -> None:
        Do not return anything, modify nums1 in-place instead.
        n = len(nums2)
        # remove 0 from nums1
        nums2_index = 0

        for i, num in enumerate(nums1):
            # We've reached the end of nums2, means we're merged.
            if nums2_index == n:
            if nums2[nums2_index] <= num:
                nums1.insert(i, nums2[nums2_index])
                nums2_index += 1

        # If there's anything left in nums2 we'll add it to the end
        for item in nums2[nums2_index:]:

if __name__ == '__main__':
    s = Solution()
    a = [1,2,3]
    s.merge(a, [2,5,6])

Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.