6gDav LeetCode Sulotions

Welcome to my LeetCode solutions collection!

Description

This is a collection page for my LeetCode solutions.
I created this because GitHub’s landing page lacks a "pinning" option (for all my repos),
and I wanted a place to organize my code.
I mainly solve LeetCode problems for practice and to learn complex algorithms, rather than for job interviews.


Choose a LeetCode:

LeetCode 1.

Two Sum

Programing langeuge: Rust 🦀

My sulotions ☺️


🧠 How I solved the problem

I implemented a brute-force solution using nested loops.
To avoid the "double-counting" bug (using the same element twice),
the inner loop starts from the next element relative to the outer loop.
This ensures that we only compare distinct pairs and find the target sum
without using any element more than once.


LeetCode 2.

Add Two Numbers x ➕ ⅹ

Programing langeuge: Rust 🦀

My sulotions ☺️


🧠 How I solved the problem

I reversed both input lists, then iterated through the elements and
converted them to strings. After concatenating them, I parsed the final
string back into an i32 to get the full number.
I did this for both lists, added the two numbers together,
and finally converted the sum back to a reversed list of characters.


LeetCode 4.

Median of Two Sorted Arrays 🖖

Programing langeuge: Rust 🦀

My sulotions ☺️


🧠 How I solved the problem

First, I concatenated and sorted the given lists.
Then, I implemented the following logic: if the combined list length is even,
I split it in the middle and calculate the average of the two central elements.
If the length is odd, I simply return the middle element.


LeetCode 5.

Longest Palindromic Substring 🤥

Programming language: Rust 🦀

My solutions ☺️


🧠 How I solved the problem

First, I return the original string if it is equal to its reversed version to avoid unnecessary processing. After this, I implemented a for loop that continuously compares a new string with its own reversed version. The backward logic follows the same principle. The last one is a bit of a silly attempt, but I'll leave it in.


LeetCode 54.

Spiral Matrix

Programming language: Rust 🦀

My solutions ☺️


🧠 How I solved the problem

I created a while loop that runs until the matrix becomes empty or the first row runs out. In the while loop I implemented four steps: 1. remove the current first row and add it to the solution vector 2. iterate through all rows and remove all last elements while pushing these to the vector 3. remove the entire last row reverse it and add it too to the solution 4. add every first element while remove these too As you see my solution based on remove and cloning.


LeetCode 6.

Zigzag Conversion

Programming language: Rust 🦀

My solutions ☺️


🧠 How I solved the problem

First, I implemented an early return for cases with only a single character. Then, I created an array of strings (acting as a matrix) based on the number of rows. I iterated through the input, pushing each character into the appropriate row index. The logic relies on the 'step' variable that increments or decrements the 'current_row' index. This 'step' changes whenever we reach the top or bottom row, determining where the next character is placed.


LeetCode 4.2.

Median of Two Sorted Arrays 🖖

Programming language: Dart 🎯

My solutions ☺️


🧠 How I solved the problem

First, I concatenated and sorted the given lists. Then, I implemented the following logic: if the combined list length is even, I split it in the middle and calculate the average of the two central elements. If the length is odd, I simply return the middle element.


LeetCode 7.

Reverse Integer 🔄

Programming language: Rust 🦀

My solutions ☺️


🧠 How I solved the problem

I implemented an if-else expression with two branches. In the first case, if the input is negative, I take its absolute value, convert it to a string, reverse it, and return it to the variable. The else block does the same but without the absolute value. Finally, I parsed the resulting string back into an integer.


LeetCode 9.

Palindrome Number 6️⃣7️⃣

Programming language: Rust 🦀

My solutions ☺️


🧠 How I solved the problem

I implemented a string-based solution. First, I converted the number to a string for easier handling. After this, I used an early return to return true if the string is only one character long. Finally, I return a boolean based on whether the string matches its reversed version.


LeetCode 8.

String to Integer (atoi)

Programming language: Rust 🦀

My solutions ☺️


🧠 How I solved the problem

First of all, I broke down the "Atoi" conversion behavior into pieces. The first piece was removing the unnecessary whitespace from the very beginning of the string by creating a new string. After this, I implemented an early return statement if the new string is empty. I check the first character of the new string: if it is a non-digit character and not '+' or '-', I return a falsy value, which is 0. I implemented another check for the first element to filter out sign duplications. If the sign is duplicated (like '++'), I return 0 as well. I iterate through the new string and examine all characters. If the character is a number, I add it to the newly created variable. If the character is not a number (even if it's a sign in this case), I break out of the loop. The next step is the type casting. I created 4 variables: two of them help with the casting, and the others are for the state. I iterate through the string where the numbers are stored and push each element to another string. I try to convert this string to a 64-bit integer. If it's successful, I check the previously declared sign boolean. If it's true, I check if the converted integer is larger than the maximum value of a 32-bit integer; if so, I return the maximum 32-bit integer. In the case of a negative number, I do the same but with the minimum of a 32-bit integer. Finally, I parse the successfully converted value to a 32-bit integer and return it.


LeetCode 7.2.

Reverse Integer 🔄

Programming language: Golang 🐹

My solutions ☺️


🧠 How I solved the problem

I implemented an if-else expression with two branches. In the first case, if the input is negative, I take its absolute value, convert it to a string, reverse it, and return it to the variable. The else block does the same but without the absolute value. Finally, I parsed the resulting string back into an integer.


LeetCode 1.2.

Two Sum

Programming language: Golang 🐹

My solutions ☺️


🧠 How I solved the problem

I implemented a brute-force solution using nested loops. To avoid the "double-counting" bug (using the same element twice), I implemted a condition that continue the loop if the "indexes" are equal with eacother. This ensures that we only compare distinct pairs and find the target sum without using any element more than once.


LeetCode 11.

Container With Most Water 🥛

Programming language: Rust 🦀

My solutions ☺️


🧠 How I solved the problem

I used a two-pointer based solution, so I iterate through the given list from the left and the right sides. I implemented an infinite loop that breaks if the left index is greater than or equal to the right index. The loop changes the iteration helper variables according to the relation between the two current values of the list. The actual width is the difference between the right and the left indices. The actual height is the current minimum value of the bounded list elements. The actual area is the product of the actual height and actual width. If this value is greater than the previous one, then the variable will be updated with this new value.


LeetCode 11.2.

Container With Most Water 🥛

Programming language: Golang 🐹

My solutions ☺️


🧠 How I solved the problem

I used a two-pointer based solution, so I iterate through the given list from the left and the right sides. I implemented an infinite loop that breaks if the left index is greater than or equal to the right index. The loop changes the iteration helper variables according to the relation between the two current values of the list. The actual width is the difference between the right and the left indices. The actual height is the current minimum value of the bounded list elements. The actual area is the product of the actual height and actual width. If this value is greater than the previous one, then the variable will be updated with this new value.


LeetCode 12.

Integer to Roman 🏛️

Programming language: Rust 🦀

My solutions ☺️


🧠 How I solved the problem

First of all, I created a loop that iterates backward. I defined a place variable using the iterator and the length of the string (which is basically a standard index, but calculated from the end). This variable provides the place value (ones, tens, etc.) for a match expression that evaluates every possibility. For example, the last index in the loop (which represents the ones place, or 0 in this case) triggers the 'ones' branch. This branch checks the character at the current index and returns the corresponding Roman numeral string, which is then prepended to the result container.


LeetCode 12.2.

Integer to Roman 🏛️

Programming language: Golang 🐹

My solutions ☺️


🧠 How I solved the problem

First of all, I created a loop that iterates backward. I defined a place variable using the iterator and the length of the string (which is basically a standard index, but calculated from the end). This variable provides the place value (ones, tens, etc.) for a match expression that evaluates every possibility. For example, the last index in the loop (which represents the ones place, or 0 in this case) triggers the 'ones' branch. This branch checks the character at the current index and returns the corresponding Roman numeral string, which is then prepended to the result container.