LeetCode-9-Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

 

判斷一個整數是否是迴文數。

 

思路:求出數字abcd的逆序的數值dcba,如果是迴文數的話,那麼abcd==dcba。

 

時間複雜度:O(n)

 

python代碼:

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        temp = x
        revert_x = 0
        while temp > 0:
            revert_x = revert_x*10 + temp % 10
            temp //= 10
        return revert_x == x


博客園博客:欠扁的小籃子

發佈了82 篇原創文章 · 獲贊 101 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章