LeetCode: Arrays 101: Inserting Items Into an Array

Hello,

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.

https://leetcode.com/problems/duplicate-zeros/

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)
                arr.pop()
                index += 1
            index += 1

 Merge Sorted Array

Given two sorted arrays, merge them together into nums1.

https://leetcode.com/problems/merge-sorted-array/

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:
                break
            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:]:
            nums1.append(item)


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

Thank you!

LeetCode: Add Two Numbers Python 3 iterative solution

[Problem Link]

Hello,

Here’s my solution for the add two numbers problem on LeetCode.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

When doing problems on websites such as LeetCode, I like to code in my own IDE, that’s why you’ll see some additional helper functions besides the solution code.

class Solution(object):
    def _get_append_func(self, root):
        _root = root
        def __add_to_list(value):
            nonlocal _root
            _root.next = ListNode(value)
            _root = _root.next
        return __add_to_list

    def _compute(self, result, base=10):
        carry = result // base
        rest = result % base
        return carry, rest

    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        root_node = None
        root_append_func = None

        term_1 = l1
        term_2 = l2
        saved_carry = 0
        while True:
            # end of expression ; add cary if any
            if term_1 is None and term_2 is None:
                if saved_carry > 0:
                    root_append_func(saved_carry)
                break

            # We haven't reached the end, we assume that whatever term is left is 0
            if term_1 is None:
                term_1 = ListNode(0)
            if term_2 is None:
                term_2 = ListNode(0)

            # add the two terms
            term_sum_carry, term_sum_no_carry = self._compute(term_1.val + term_2.val)

            # add sum to current carry
            next_carry, total_with_carry = self._compute(term_sum_no_carry + saved_carry)

            # save carry
            saved_carry = term_sum_carry + next_carry

            # save total
            if not root_node:
                root_node = ListNode(total_with_carry)
                root_append_func = self._get_append_func(root_node)
            else:
                root_append_func(total_with_carry)

            # move on
            term_1 = term_1.next
            term_2 = term_2.next

        return root_node

My solution isn’t that great because of the _add_to_list function which iterates the root_node to append the next value. I have no idea how to optimize that right now.

UPDATE:

I’ve learnt about nonlocal and found a way to slightly optimize my code:

    def _get_append_func(self, root):
        _root = root
        def __add_to_list(value):
            nonlocal _root
            _root.next = ListNode(value)
            _root = _root.next
        return __add_to_list

Thanks for reading!