Leetcode – Add Two Numbers

Name: Add Two NumbersDifficulty: MediumDescription: Add two numbers stored in reverse in a linked list Example: Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807. Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] Output: [8,9,9,9,0,0,0,1] Initially when solving this problem, I first made sure the lists were the same size […]

Leetcode – Add Binary

Name: Add BinaryDifficulty: EasyDescription: Given two binary strings a and b, return their sum as a binary string. Example: Input: a = “1010”, b = “1011” Output: “10101” This one stretched the memory. We add bits from right to left, bit by bit, so the first thing we need to do is reverse the string […]

Leetcode – Plus One

Name: Plus OneDifficulty: EasyDescription: Increment the large integer by one and return the resulting array of digits. Example: Input: digits = [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. Incrementing by one gives 4321 + 1 = 4322. Thus, the result should be [4,3,2,2]. Feels like I hacked this one, I also modified […]

Leetcode – Maximum Subarray

Name: Maximum SubarrayDifficulty: EasyDescription: Find the value of the largest contiguous subarray. Example: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. This one, although simple, tripped me up. I was using Brute Force but it was too slow. I kept getting timeouts. Some of the tests use arrays with […]

Leetcode – Search Insert Position

Name: Search Insert PositionDifficulty: EasyDescription: Return the index of target, or where target should be in a sorted array Example: Input: nums = [1,3,5,6], target = 5 Output: 2 Input: nums = [1,3,5,6], target = 2 Output: 1 Input: nums = [1,3,5,6], target = 7 Output: 4 The solution was quite simple, but I complicated […]

Leetcode – strStr()

Name: strStr()Difficulty: EasyDescription: Find the needle in the haystack, return the first index of or -1 if not found. Example: Input: haystack = “hello”, needle = “ll” Output: 2 Interestingly, I used simple indexing in this solution rather than iterators. I suspect, whilst very fast, the iterators were the cause of the higher memory consumption […]

Leetcode – Remove Element

Name: Remove ElementDifficulty: EasyDescription: Remove all instances of val from nums, modify in place, return new count. Example: Input: nums = [0,1,2,2,3,0,4,2], val = 2 Output: 5, nums = [0,1,4,0,3,_,_,_] After count (5 in this case), the rest of the nums are ignored. This is a simple solution, the idea is to have two indexes. […]

Leetcode – Remove Duplicates

Name: Remove Duplicates from Sorted ArrayDifficulty: EasyDescription: Remove duplicates from a sorted array – in place Example: Input: nums = [0,0,1,1,1,2,2,3,3,4] Output: 5, nums = [0,1,2,3,4,_,_,_,_,_] Should return the correct number (k) of unique numbers, the array should be in sorted order. After k numbers, the rest of the numbers are ignored.

Leetcode – Merge Two Sorted Lists

Name: Merge Two Sorted ListsDifficulty: EasyDescription: Merge two linked lists in sorted order Example: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Input: list1 = [], list2 = [] Output: [] Input: list1 = [], list2 = [0] Output: [0] The excellent STL does all the hard work. I push the numbers from the […]