Leetcode 9: Palindrome Number

grid47
grid47
Exploring patterns and algorithms
Nov 6, 2024 5 min read

A number with mirrored halves, radiating a soothing glow as it reflects symmetrically.
Solution to LeetCode 9: Palindrome Number Problem

Given an integer, return true if the number is a palindrome. A number is a palindrome if it reads the same forwards and backwards. If the number is negative, it is automatically not a palindrome.
Problem
Approach
Steps
Complexity
Input: The input is a single integer x. The number can be negative or positive.
Example: Input: x = 121
Constraints:
• -231 <= x <= 231 - 1
Output: Return a boolean value indicating whether the input integer is a palindrome or not.
Example: Output: true
Constraints:
• Return true if the number is a palindrome, false otherwise.
Goal: To check if a given number is a palindrome without converting the integer to a string.
Steps:
• Check if the number is negative. If it is, return false immediately.
• Reverse the digits of the number.
• Compare the original number with the reversed number. If they are the same, return true; otherwise, return false.
Goal: The number x can be a negative or positive integer, and the solution must handle both cases efficiently.
Steps:
• The number should be within the range of a 32-bit signed integer (-2^31 <= x <= 2^31 - 1).
• The solution should not convert the integer to a string.
Assumptions:
• We are guaranteed that the input will be within the specified range (-231 <= x <= 231 - 1).
• Negative numbers are not palindromes.
Input: Input: x = 121
Explanation: The number 121 reads the same from left to right and right to left, so it is a palindrome.

Input: Input: x = -121
Explanation: The number -121 is not a palindrome because the negative sign appears only on the left, not the right.

Input: Input: x = 10
Explanation: The number 10 is not a palindrome because it reads '01' from right to left, which is not the same as the original number.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus