Palindrome Number
The key idea
Any negative number is never a palindrome because of the leading minus sign, and any positive number ending in
0 (except 0 itself) is never a palindrome. For the rest, you only need to reverse the second half of the digits and compare it to the first half — you never have to build the whole reversed number, which also dodges integer overflow.Problem
Given an integer x, return true if x is a palindrome, and false otherwise.
A number is a palindrome when it reads the same forwards and backwards. For example, 121 is a palindrome while 123 is not.
Constraints
-2^31 <= x <= 2^31 - 1- Follow up: solve it without converting the integer to a string.
Examples
Input: x = 121
Output: true
Input: x = -121
Output: false
Input: x = 10
Output: false
Complexity
Time: O(log10(x)) Space: O(1)
See the full solution
- ✓Full worked approach
- ✓Reference code in 5 languages
- ✓Problem-solving tips
- ✓Step-by-step animated visualization
More Math / Number Theory problems
- Add BinaryEASY
- Factorial Trailing ZeroesMEDIUM
- Excel Sheet Column TitleEASY
- Greatest Common Divisor of StringsEASY
- Integer to RomanMEDIUM
- Multiply StringsMEDIUM
- Plus OneEASY
- Reverse IntegerMEDIUM
- Roman to IntegerEASY