#9

Palindrome Number

Easy Digit reversal

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Decide whether an integer reads the same forwards and backwards, without converting it to a string.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: do the digits read the same from both ends, without turning the number into a string?

Why this works: build the reverse of the second half digit by digit. When the reversed half catches up with the front half, compare them. Half is enough, and it can't overflow.

Example: x = 1221

x=1221, rev=0 → rev=1, x=122x=122,  rev=1 → rev=12, x=12x == rev (12 == 12) → palindrome

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Reversing the whole number into an int, which can overflow for large inputs.