普通的判断方式,i与j两个指针,从头尾移动。
def is_huiwen(src):
if len(src) <= 1:
return True
i, j = 0, len(src)-1
while i<j:
if src[i] == src[j]:
i += 1
j -= 1
else:
return False
return True
递归方式。
def is_huiwen(src):
if len(src) <= 1:
return True
if src[0] == src[-1]:
return is_huiwen(src[1:-1])
else:
return False
print(is_huiwen('abca'))