# Leetcode – Length of Last Word

Name: Length of Last WordDifficulty: EasyDescription: Find the length of the last word Example: Input: s = ” fly me to the moon ” Output: 4 Explanation: The last word is “moon” with length 4.

# 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 […]

# Leetcode – Valid Parentheses

Name: Valid ParenthesesDifficulty: EasyDescription: Return true if the string opens and closes parenthesis in the correct order Example: Given “()”, return true Given “()[]{}”, return true Given “{[]}”, return true Given “(]”, return false The stack data structure is very useful. As long as the character is an opening parenthesis, i.e. ({[, we just keep […]

# Leetcode – Longest Common Prefix

Name: Longest Common PrefixDifficulty: EasyDescription: Find the longest common prefix amongst several words Example: Given [“flower”, “flow”, “flight”], return “fl” Given [“car”, “tree”], return “” Given [“c”] return “c” I was pretty tired when coding this one, time for bed ðŸ™‚

# Leetcode – Roman to Integer

Lesson learned: Do it right the first time ðŸ™‚ Name: Roman to IntegerDifficulty: EasyDescription: Convert a Roman Numeral to an Integer Example: Given III, return 3 Given MCMXCIV, return 1994 I was a little lazy on this one, this was the third for the evening and my brain was checking out. I chose to replace […]