Leetcode:9.判断回文数

字符串操作有许多地方和列表是相似的,比如切片,倒序输出等,一定要灵活运用。

def isPalindrome(num):
    if num<0:
        print("%d is not a Palindrome"%num)
    else:
        y = str(num)[::-1]
        if y == str(num):
            print("%d is a Palindrome"%num)
        else:
            print("%d is not a Palindrome"%num)
isPalindrome(121)
isPalindrome(-121)

输出:121 is a Palindrome         -121 is not a Palindrome

你可能感兴趣的:(leetcode)