In this article, I share only my thought process for day 3's Leetcode challenge for the #I4G10DaysOfCodeChallenge series. To curb the occurrence of plagiarism we've been advised to not share our solutions completely. This was a relatively interesting problem.
Let's have a look...
Problem statement, examples, and constraints.
Given an integer
x is a palindrome integer.
An integer is a palindrome when it reads the same backward as forward.
121 is a palindrome while
123 is not.
Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left.
Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Input: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
-231 <= x <= 231 - 1
So like I said earlier, this was a relatively easy problem to solve. I resorted to using the two-pointer approach.
I first converted the number to a string using the
toString inbuilt function, then I created two tracking variables to iterate through the string from both ends.
On each iteration, I check if the value at the start of the string is not the same as the value at the end, if it is not the same I return false immediately, But if it is the same, then I'd continue my iteration.
If the values end up being the same all through, when I get to the middle of the string I'd return true because I'd have looked at all the values.
This process would have been explained better with code snippets, forgive me. I'm just following instructions.
Thanks for reading through, I hope you learned a thing or two. Will see you in the next challenge post.
Please share your thoughts in the comment section, let's talk :-)
Your friend in progress,