码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-11-18 14:27:44      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:div   ret   pre   res   style   回文   return   个数   src   

判断一个数是否为回文数,回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

通常让数字逆序,然后判断和原数字是否相等,这里只需逆序一般就可以。

case1.奇数位例如判断12321

while循环到x=12   res = 123     x!>res   跳出循环

res //10   ==    x 为True

case2.要判断的数位数为偶数位  :1221

x=12  res=12    x !.>res  

res == x  True 

 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

技术图片

 

 

 

 

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

标签:div   ret   pre   res   style   回文   return   个数   src   

原文地址:https://www.cnblogs.com/NPC-assange/p/11881841.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!