Problem Statement in English

You’re given an integer x. Return true if x is a palindrome, and false otherwise.


Approach

We can determine if an integer is a palindrome by converting it to a string and checking if the string is equal to its reverse. If they are equal, then the integer is a palindrome; otherwise, it is not.

And we’re done!


Solution in Python


class Solution:
    def isPalindrome(self, x: int) -> bool:
        return str(x) == str(x)[::-1]

Complexity

  • Time: $O(n)$

  • Space: $O(n)$
    Since we are converting the integer to a string, the space complexity is linear with respect to the number of digits in the integer.

We can also solve this problem without converting the integer to a string, which would reduce the space complexity to $O(1)$.


And we are done.