回文数

作者: _阿南_ | 来源:发表于2020-03-01 17:31 被阅读0次

题目:

判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
示例 1:
输入: 121
输出: true
示例 2:
输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
示例 3:
输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。
进阶:
你能不将整数转为字符串来解决这个问题吗?

题目的理解:

转为字符串,然后颠倒。

python实现

class Solution:
    def isPalindrome(self, x: int) -> bool:
        x_str = str(x)
        result = list()
        for c in x_str:
            result.append(c)

        result.reverse()
        reverse_str = ''.join(result)
        
        return x_str == reverse_str

提交

没进阶啊

// END 同类型的题目,做的太多了会吐。

相关文章

网友评论

    本文标题:回文数

    本文链接:https://www.haomeiwen.com/subject/knthkhtx.html