Leetcode练习(Python):数学类:第9题:回文数:判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

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

进阶:

你能不将整数转为字符串来解决这个问题吗?

思路:

思路一:将整数转为字符串。

思路二:直接颠倒数字。

程序1:

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x < 0:
            return False
        x1 = str(x)[::-1]
        x2 = int(x1)
        if x == x2:
            return True
        else:
            return False
 
思路2:
class Solution:
    def isPalindrome(self, x):
        if x < 0:
            return False
        x1 = x
        x2 = 0
        while x > 0:
            x2 = x2 * 10 + x % 10
            x = x // 10
        return x1 == x2
原文地址:https://www.cnblogs.com/zhuozige/p/12831346.html