LeetCode--009--回文数(python)

判断一个数是否为回文数,回文数是指正序(从左向右)和倒序(从右向左)读都是同样的整数。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

相关文章
相关标签/搜索