Python 9.回文数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Scrat_Kong/article/details/82431668

 回文数Python实现

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """

        temp = str(x)[::-1]
        if(temp == str(x)):
            return True
        else:
            return False

 复杂低幼版:

#  python
class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        str1 = str(x)
        if str1[0] == '-':
            print('false')

        for i in range(int(len(str1) / 2) + 1):
            if i == len(str1) - i - 1 or (str1[i] == str1[len(str1) - i - 1] and (i + 1) == (len(str1) - i - 1)):
                print('true')

            if str1[i] != str1[len(str1) - i - 1]:
                print('false')
                break

猜你喜欢

转载自blog.csdn.net/Scrat_Kong/article/details/82431668