Accepted Python code, brute force compare half digits, no reverse


  • 0
    S

    Python code. Brute force compare half of the digits. No reverse integer. I don't think there is overflow...

    class Solution(object):
        def isPalindrome(self, x):
            """
            :type x: int
            :rtype: bool
            """
            import math
            if x < 0:
                return False
            elif x == 0:
                return True
            else:
                digits = int(math.log10(x)) + 1
                i = 1
                while i <= int(0.5 * digits):
                    low = int(x/(10**(i-1)))%10
                    high = int(x/(10**(digits-i)))%10
                    if low != high:
                        return False
                    i += 1
                return True
    

Log in to reply
 

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