9. 回文数

https://leetcode.cn/problems/palindrome-number

难度:简单

思路:利用切片将x进行倒序,和正序的x进行比对。

自己的写法(思路不负责,但是写得很复杂):

class Solution:
    def isPalindrome(self, x: int) -> bool:
        x = str(x)
        newX = x[::-1]
        if newX == x:
            return True
        else:
            return False

执行用时5ms,击败75.14%

还是上面的思路,简化写法:

class Solution:
    def isPalindrome(self, x: int) -> bool:
        return str(x) == str(x)[::-1]

执行用时3ms,击败91.84%

posted @ 2025-11-12 21:58  YouKong  阅读(4)  评论(0)    收藏  举报