Discuss / Python / My answer

My answer

Topic source

winner丶John

#1 Created at ... [Delete] [Delete and Lock User]

def is_palindrome(n):

    n = str(n)#直接采用python内置的转字符串函数str()

    length = len(n)#数字变成字符串后的长度

    index = 0#代表对称组号,如index = 0,下列循环代表判断首/尾

    while n[index] == n[length-1-index]:#判断字符串前后两部分对称组数字是否相等

        if length-1 - index*2 <= 2:#如果对称组的两数字间距小于等于2

            return True#返回此数字是回数

        index += 1#每次判断对称组间距后,更换对称组

    return False#没有完成对称判断直至中间,即非对称数字


  • 1

Reply