思路
- 因为要判断整数是否为回文, 则去除正负号
- 字符串判断是否为回文字符串
编写
class Solution:
def isPalindrome(self, x: int) -> bool:
if(x == None or x<0): return False
x = str(x)
xLen = len(x)
for i in range(xLen//2):
if(x[i] != x[xLen-i-1]):
return False
return True
思考与学习
既然已经把整数转换为字符串了,那为何不利用python的切片特性
class Solution:
def isPalindrome(self, x: int) -> bool:
x = str(x)
return True if(x == x[::-1]) else False
人生苦短,我用 Python …