思路
- 因爲要判斷整數是否爲迴文, 則去除正負號
- 字符串判斷是否爲迴文字符串
編寫
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 …