AAlgoLoopSpaced repetition for LeetCode
EASYMath / Number TheoryLeetCode ↗

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

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

410310
Step-by-step visualization
Start free →

More Math / Number Theory problems