0
下面的代码是针对Leetcode上的编码练习 - 我检查一个数字是否是回文(不将它转换为字符串)。我无法理解为什么像11号,22,33等检查一个数字是否是回文 - 11,22等失败
def isPalindrome(self, x):
#x is an integer
digit = 0
reverse = 0
#Single digit numbers will be palindromes
if x >= 0 and x < 10:
return True
#fetch individual digits and build the reverse number
while x > 0:
digit = x % 10
reverse = (reverse * 10) + digit
x = x/10
if x == reverse:
return True
else:
return False
您在循环中反复修改'x',然后尝试在之后的比较中使用它。这永远不会为多位数字返回“真”。 – Phylogenesis
@Phylogenesis这也是事实,但还不够。 OP,在你心目中运行代码22 ..你没有注意到它有什么问题吗?像'reverse =(reverse * 10)+ digit'还是'x = x/10'? –
我有多傻!感谢您清除那个 – user1528508