9. Palindrome Number
Easy
Question
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Example 2:
Example 3:
Follow up:
Could you solve it without converting the integer to a string?
Revert half of the number
For example, input is 1221
, if we revert last part of the number 1221 from 21 to 12, and compare with first half.
When
x <= reversed number
, we reach the halfTwo cases:
"1221" pattern,
x == rev
."121" pattern,
x == rev / 10
, get rid of the middle digit
Time complexity: O(log_10(n))
Space complexity: O(1)
Code
Last updated
Was this helpful?