1. 程式人生 > >Leetcode (四) 迴文數

Leetcode (四) 迴文數

迴文數:

判斷一個整數是否是迴文數。迴文數是指正序(從左向右)和倒序(從右向左)讀都是一樣的整數。

1. 轉成字串解題

class Solution:
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x<0:   #所有負數都不是迴文數
            return False
        else:
            a = str(x)  #轉成字串
            b = a[::-1]  #倒序排列
            if a==b:
                return True
            else:
                return False