Advertisement

Palindrome Number - LeetCode 9 Solution

Palindrome Number - Complete Solution Guide

Palindrome Number is LeetCode problem 9, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer x , return true if x is a palindrome , and false otherwise . 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: -2 31 <= x <= 2 31 - 1

Detailed Explanation

The problem asks you to determine if an integer is a palindrome. A palindrome is a number that reads the same forwards and backward. For example, 121 is a palindrome, but 123 is not. The input is a single integer `x`, and the output is a boolean value (`true` if it's a palindrome, `false` otherwise). The constraints specify that the input integer will be within the range of a 32-bit signed integer.

Solution Approach

The provided solution uses a clever iterative approach to check for palindromes without converting the integer to a string. It reverses only the first half of the number and compares it to the remaining part of the original number. This significantly improves efficiency compared to complete reversal.

Step-by-Step Algorithm

  1. Step 1: Handle negative numbers and zero. Negative numbers are immediately deemed non-palindromes, and 0 is a palindrome.
  2. Step 2: Check if the number ends in 0 (except for 0). If it does (and it's not 0), it can't be a palindrome.
  3. Step 3: Initialize a `reversed_num` variable to 0. This will store the reversed half of the input number.
  4. Step 4: Iterate while the original number `x` is greater than `reversed_num`. In each iteration:
  5. Step 5: Extract the last digit of `x` using the modulo operator (`% 10`).
  6. Step 6: Append this last digit to `reversed_num` by multiplying it by 10 and adding it to `reversed_num`.
  7. Step 7: Remove the last digit from `x` using integer division (`// 10`).
  8. Step 8: After the loop, check if `x` (the remaining half) is equal to `reversed_num` or if `x` is equal to `reversed_num / 10` (to handle odd-length numbers). If either is true, it's a palindrome.

Key Insights

  • Insight 1: Negative numbers are not palindromes because of the minus sign. This allows for an early exit condition.
  • Insight 2: Numbers ending in 0 (except 0 itself) are not palindromes because they would have a leading 0 if reversed. This also helps with early exit.
  • Insight 3: We only need to compare the first half of the number with its reverse. Reversing the entire number is unnecessary and potentially inefficient for very large numbers.

Complexity Analysis

Time Complexity: O(log(x))

Space Complexity: O(1)

Topics

This problem involves: Math.

Companies

Asked at: Accenture, Adobe, Amazon, Apple, Bloomberg, Capgemini, Capital One, Cognizant, Deloitte, EPAM Systems, FPT, Garmin, HCL, IBM, Infosys, Intel, J.P. Morgan, Meta, Microsoft, Oracle, Qualcomm, Roche, Samsung, Uber, Visa, Walmart Labs, Wipro, Yahoo, Yandex, Zoho, persistent systems, tcs.