题目来源:https://leetcode.com/problems/palindrome-number/description/
题意分析:判断一个数字是否是回文数,回文数即是左右对称的数字,例如1331。注意,负数不是回文数。
以下通过一行代码实现,由此可看出python的强大功能。
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
return int(str(abs(x))[::-1]) == x
由于本人是python小白,准备通过做题有目的性的学习知识,所以不懂x[::-1],通过查阅资料,x[::-1]是将字符串进行反转。
例如:
s='hello'
print s[::-1]
输出:
olleh
x[I:J:K]表示索引x对象中的元素,从偏移为I直到偏移为J-1,每隔K个元素索引一次。
例如:
s='abcdefghijklm'
print s[1:10:2]
输出:
bdfhj