判斷一個數是否爲迴文數,迴文數是指正序(從左向右)和倒序(從右向左)讀都是同樣的整數。spa
一般讓數字逆序,而後判斷和原數字是否相等,這裏只需逆序通常就能夠。code
case1.奇數位例如判斷12321blog
while循環到x=12 res = 123 x!>res 跳出循環io
res //10 == x 爲Trueclass
case2.要判斷的數位數爲偶數位 :1221循環
x=12 res=12 x !.>res im
res == x True img
1 class Solution: 2 def isPalindrome(self, x: int) -> bool: 3 if x < 0 or (x % 10 == 0 and x != 0): 4 return False 5 res = 0 6 while x>res: 7 res = res * 10 + x%10 8 x //=10 9 if res == x or res//10 ==x: 10 return True 11 return False