Leetcode

Longest Common Prefix

If given X number of strings find the longest common prefix within them. Such as “bad, bar, bank” would return ‘ba’.

Problem on leetcode

My Solution on Github

Solution explanation: While inelegant it does work. I intend on returning to this at some point to find a much more efficient solution

Roman Numerals to Decimal

If given a roman numeral translate it to the decimal value. Such as ‘IV’ would return ‘4’ but ‘VI’ would return ‘6’.

Problem on leetcode

My Solution on Github

Solution explanation: Firstly roman numerals are weird because IV = 4 and VI = 6 so I couldn’t just directly say the letter and put it as a number and add them all up. So it was more complicated than it first appears. So I set up 2 Array List’s 1 for the roman numerals split into individual characters and one for the corresponding decimal values. Then I put the Array list of decimals into a loop looking for if the current number is less than the next one to subtract the smaller number from bigger number (IE in the cause of IV, it would show [1, 5] so the current number [1] is less than the next [5]) then add that to the solution. IF it isn’t then it can be assumed the value can just be added to the answer without any formula.

Another way to approach this question is to use a map so I may do that in the future.

Palindrome Number

If given an integer determine if it is a Palindrome number. If it is the same forward and backwards like 1001 or 796697.

Problem on leetcode

My Solution on Github

Solution explanation: I am very proud of this solution basically I took advantage of the Stringbuilder.reverse() method to reverse a string. The only thing I had to do was convert the Integer to a string and then the rest was super easy. This might be the most elegant solution I’ve done since starting to do these problems.

Two Sum

If given an array of integers and another integer target, return the index of the first 2 elements that sum up to the target. If given [2, 4, 5] with a target 9 return[1, 2] where 4 + 5 = 9 and they’re the first and second element of the 0 indexed array.

Problem on leetcode

My Solution on Github

Solution explanation: I enter a nested loop with where the first loop looks at the first value and subtracts that from the target then it iterates through the rest of the array looking to see if one get’s the new target down to 0 if not the target is put back at it’s initial value and the next element of the array is subtracted and looks at all the next values and so on until there is finally a match getting the target to 0.

Reverse Integer

If given an integer return the numbers in reverse. I.E. 123 becomes 321, negatives however stay negative so -698 becomes -896. If the value causes an overflow return 0.

Problem on leetcode

My Solution on Github

Solution explanation: This is very similar to the palindrome number problem solved above. The main difference is how negatives are handled, in palindrome they’re treated as never palindrome but for this one you have to reverse it but keep the negative sign in front. So I added a new boolean to tell if the number is negative or not. Then I took the absolute value of it. Then I did exactly what I did with the palindrome numbers and reversed it with StringBuffer and finally if it was negative I times it by negative 1 to get the negative back and return that value.

Merge Sorted Array

Given 2 sorted arrays, merge them and sort the new array

Problem on leetcode

My Solution on Github

Solution explanation: A simple solution for this, simply take the 2 arrays put them into an ArrayList and use the collections.sort() method. Then put them back into an array and return that result.

Remove Element in place

Given an array, remove all elements of a chosen int in place I.E. without creating a new data structure

Problem on leetcode

My Solution on Github

Solution explanation: This is a challenging one due to needing to change the elements in place. It’d be easy in theory to just make a new array, but that’s not an option with this task. So instead I adjusted the existing array. Setting the elements that aren’t removed to the front and the others to the back. Then I simply return how many elements passed the test.

Remove Duplicates From Sorted Array

Given an array, remove all elements that are duplicated

Problem on leetcode

My Solution on Github

Solution explanation: Basically the same as above. But instead of looking for a certain number it now searches through the array to look at the next number in the array to see if it’s a duplicate.

Remove Duplicates From Sorted Array Part 2

Given an array, remove all elements that are duplicated twice

Problem on leetcode

My Solution on Github

Solution explanation: Using the same algorithm as above I managed to go through the array, I then added a variable to keep track of how many duplicates in a row there are to allow 2 in a row

Majority Element

Given an array, display the element that appears (n/2)+1 times

Problem on leetcode

My Solution on Github

Solution explanation: I iterated through the array and looked at each element and kept track of them using the count variable. Once something was found to be majority, it ended the loop.