Simple python solution with recursion, no need to handle overflow


  • 0
    3
    def check(head,tail):
        if head==0:
            return True
        h_num=head%10
        if not check(head/10,tail):
            return False
        t_num=tail[0]%10
        tail[0]=tail[0]/10
        return h_num==t_num
    class Solution(object):
        def isPalindrome(self, x):
            if x < 0:
                return False
            return check(x,[x])
    

    Description:
    Similar to DFS, go to the last digit, then peel one digit from the starting and compare two num.


Log in to reply
 

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