simple Python,without conversion and overflow risk


  • 0
    Z

    sometimes beats 99.90%,sometimes beats even only half of it...confusing

    class Solution(object):
        def isPalindrome(self, x):
            """
            :type x: int
            :rtype: bool
            """
            if x < 0: # negative numbers
                return False
            elif x < 10: # 0~9
                return True
            elif x % 10 == 0: # numbers end with 0
                return False
            n = 0
            while x > n: # only compare half of x, reducing the expense
                n = n*10 + x%10
                x = x // 10
            return x == n or x == n // 10 # one for even numbers and another for odd numbers

Log in to reply
 

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