問題:迴文數的判斷
分析:判斷一個數是否爲迴文數,首先先判斷正負,如果爲負數,那麼一定不是迴文數,
所以我們就只需考慮正數和0。然後藉助循環和,將原來的數先取餘(%),再整除,依次得到每一位數,最後累加求和,得到翻轉過來的數,與原來的數作比較,得到結果。
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
s = 0
result = x
while x > 0:
s = x % 10 + s * 10
x = x//10
if result == s:
return True
else:
return False
筆者最近在學python,所以就先拿簡單的題,試試手