代码仓库:Github | Leetcode solutions @doubleZ0108 from Peking University.
- 解法1(T62% S98%): 按照题意如果是负数直接False,如果是个位数直接True,否则通过while % //将数字反转比较
- 解法2(T8% S93%): 转为字符串判断
- 题干说了不推荐这么做
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x<0:
return False
elif x<10:
return True
rev, save = 0, x
while x:
rev = rev*10 + x%10
x //= 10
return rev == save
def otherSolution(self, x):
if x<0:
return False
return str(x)[::-1] == str(x)
本文介绍两种有效的整数回文判断方法。方法一通过数字反转进行比较,方法二利用字符串反转实现。第一种方法效率较高,适用于大多数情况,而第二种方法虽然简单直观但不被推荐。

被折叠的 条评论
为什么被折叠?



