1. 程式人生 > >leetcode迴文數isPalindrome

leetcode迴文數isPalindrome

首先想到的是將數字轉換成字串,再比較字串是否相同的方法。

class Solution:
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        
        array = str(x)
        transArray = array[::-1] #改成if str(x) == str(x)[::-1]速度更快
        if array == transArray:
            return True
        else:
            return False

事實證明此法是可行的。

 又注意到原題目有以下提示:

進階:

你能不將整數轉為字串hai來解決這個問題嗎?

 目前還沒有解決。代更。