python solution use recursion[AC]

  • 0

    Treat the input int just like string, and split it into head tail character and a shorter palindrome string, so it can be solved recursively.

    its time complexity is O(n)

    class Solution(object):
        def isPalindrome(self, x):
            :type x: int
            :rtype: bool
            x = str(x)        #  convert int to string
            if len(x) < 2: return True       # base case
            return x[0]==x[-1] and self.isPalindrome(x[1:-1])   

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.