Emmanuel Obi
The Code Prophet

Follow

The Code Prophet

Follow
Day 3 - Palindrome Number - Leetcode Problem Solved

Day 3 - Palindrome Number - Leetcode Problem Solved

#I4G10DaysOfCodeChallenge - Day 3

Emmanuel Obi's photo
Emmanuel Obi
·Sep 24, 2022·

2 min read

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.

Palindrome Number

#I4G10DaysOfCodeChallenge


Given an integer x, return true if x is a palindrome integer.

An integer is a palindrome when it reads the same backward as forward.

For example, 121 is a palindrome while 123 is not.

Example 1:

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:

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.

Example 3:

Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Constraints:

  • -231 <= x <= 231 - 1

Thought Process

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.

Closing

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,

CodeProphet.

 
Share this