【LeetCode刷题 Python】回文数

题目

给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。

回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

例如,121 是回文,而 123 不是。

输入:x = 121
输出:true
输入:x = -121
输出:false
解释:从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。

实现

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        # true false的首字母要大写 True,False
        """
        
        y=str(x)
        if len(y)==0:
            return False

        for index in range(0,len(y)/2+1):
            if y[index]!=y[len(y)-index-1]:
                return False
        return True
       
class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        return str(x) == str(x)[::-1]

总结

循坏好久,基本语法也都不知道,后面看解析一个切片就搞定了!

猜你喜欢

转载自blog.csdn.net/Magnolia_He/article/details/129327290