Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
示例 1:
1 2 |
<strong>输入:</strong> 121 <strong>输出:</strong> true |
示例 2:
1 2 3 |
<strong>输入:</strong> -121 <strong>输出:</strong> false <strong>解释:</strong> 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。 |
示例 3:
1 2 3 |
<strong>输入:</strong> 10 <strong>输出:</strong> false <strong>解释:</strong> 从右向左读, 为 01 。因此它不是一个回文数。 |
进阶:
你能不将整数转为字符串来解决这个问题吗?
解答(使用字符串方法)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def isPalindrome(x): ''' :type x: int :rtype: bool ''' str_num = str(x)[::-1] if str_num == str(x): return True else: return False num = int(input('Pls input a number:')) print(isPalindrome(num)) |
输出
1 2 |
Pls input a number:12321 True |