题目要求:判断一个整数是否为回文,
例如:Input: 121 Output: true
Input: -121 Output: false
思路:将输入转换为字符串形式,判断逆序前后二者是否相等
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
x=str(x)
y = x[: : -1]
if x == y:
return True
else:
return False
Runtime: 212 ms
代码表述累赘,参考代码如下:
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
rev_string = str(x)
reverse = rev_string[::-1]
return reverse == str(x)
Runtime: 132 ms