Skip to content

Math

Introduction

Each problem on the platform includes a direct solution, while others solely outline the approach within a function.


Solution

Source code in algorithms/math/palindrome.py
class Solution:
    def palindrome(self, num):
        """
        :type num: int
        :rtype : bool
        """
        rev = 0
        if num < 0:
            x = str(num)
            rev = x[::-1]
        while num != 0:
            x = num%10
            rev = rev *10 + x
            x/=10
        return rev == num

palindrome(num)

:type num: int :rtype : bool

Source code in algorithms/math/palindrome.py
def palindrome(self, num):
    """
    :type num: int
    :rtype : bool
    """
    rev = 0
    if num < 0:
        x = str(num)
        rev = x[::-1]
    while num != 0:
        x = num%10
        rev = rev *10 + x
        x/=10
    return rev == num