LeetCode Challenges #1: Started with a Challenge
Exploring two ‘easy’ LeetCode problems: solutions, strategies, and insights. Dive into my journey with coding challenges!
Background
It’s quite surprising that on my initial visit to LeetCode, I found myself signed up but unsure where to begin. Eventually, I opted to tackle array problems as they appeared to be more approachable. With insights from other developers and YouTube, I learned that LeetCode problems can be challenging, hence my decision to start with those labeled as “easy”.
First Problem: Two Sum
The question is like: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
Understanding the Problem
The problem statement was straightforward: given an array of integers nums and a target integer, I needed to find the indices of two numbers in the array that add up to the target. It sounded like a classic use case for a hashmap or a two-pointer approach.
Solving the Problem
So, I began by doodling my plan on paper. Then, with the help from StackOverflow and Google, I found a solution! I figured out I could use a hashmap to keep track of each number’s buddy while going through the array. Whenever I stumbled upon a new number, I just peeked into my hashmap to see if its buddy (you know, the one that adds up to the target) was already there. If it was, boom! Found my perfect pair of indices.
<?php
class Solution {
/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer[]
*/
function twoSum($nums, $target) {
$map = array();
foreach ($nums as $key => $value) {
$complement = $target - $value;
if (isset($map[$complement])) {
return [$map[$complement], $key];
}
$map[$value] = $key;
}
return null;
}
}
?>
So, I started out tackling above coding challenge with some help from Google and Stack Overflow. But, I soon realized that relying too much on them wasn’t really helping me grow. So, I decided to take matters into my own hands!
For the next challenges, I will try to ditch the search bar and craft my own problems to solve. It will be a bit challenging, but it will also be super rewarding.
Sure, I might stumble here and there, but that’s all part of the fun, right? Plus, every little win feels like a big victory when you know you did it all by yourself.
So, here’s to embracing the challenge and growing one problem at a time! 🚀
Second Problem: Palindrome Check
The question is like: Given an integer x, return true if x is a palindrome , and false otherwise.
Understanding the Problem
When I encountered the palindrome check problem, I approached it with curiosity and determination. The task seemed clear: given an integer, determine if it reads the same backward as forward. This problem didn’t just require coding skills; it demanded a deeper understanding of number manipulation and logical thinking.
Solving the Problem
I started by sketching out my approach. I realized that to check for a palindrome, I needed to reverse the digits of the number and compare the reversed number with the original one. I began by initializing variables to store the original number and its reverse:
function isPalindrome($x) {
$temp = $x;
$revDigit = 0;
Next, I entered a while loop to iterate through the digits of the number and construct its reverse:
while ($temp != 0 && $x > 0) {
$digit = $temp % 10;
$revDigit = $revDigit * 10 + $digit;
$temp = floor($temp / 10);
}
In this loop, I extracted the last digit of the number using the modulo operator and appended it to the revDigit variable. Then, I updated the $temp variable by removing the last digit, effectively moving to the next digit in the number.
Finally, I checked if the original number was equal to its reverse:
if ($x == $revDigit) {
return true;
} else {
return false;
}
If they were equal, I returned true, indicating that the number was a palindrome; otherwise, I returned false.
I submitted the solution, it got accepted but the solution was not optimal, here’s the result

I was disappointed at first, but then I tried to optimize the code to this
<?php
class Solution {
/**
* @param Integer $x
* @return Boolean
*/
function isPalindrome($x) {
if ($x < 0) {
return false;
}
// Single digit numbers are always palindromes
if (abs($x) < 10) {
return true;
}
$originalNumber = $x;
$reversedNumber = 0;
while ($originalNumber != 0) {
$lastDigit = $originalNumber % 10;
$reversedNumber = $reversedNumber * 10 + $lastDigit;
$originalNumber = floor($originalNumber / 10);
}
return $x == $reversedNumber;
}
}
?>
And guess the result? It reduced to 28 ms. The only thing I did above was to avoid unnecessary code execution. You can see the result below:

This was all about my first challenge in leetcode. I’ll be exploring more into leetcode and will keep posting.
Thank you for reading!