定义函数isPalindrome(s),接收一个字符串,判断该字符串是否是回文。是则返回True,否则返回False。
“回文”是指正读反读都能读通的句子,如“我为人人,人人为我”等。
在数学中也有这样一类数字有这样的特征,称为回文数。如:131,13431等。
!!我搞不太明白

回文然后定义函数,我不明白回文
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
关注
回文字符串就是一个字符串,倒过来和原字符串一样,这样的字符串就叫回文字符串。比如121,他倒过来还是121。算法如下:
def isPalindrome(s): length = len(s) if not length: # 空字符串 return True mid_index = length // 2 # 如果s长度为奇数则是中点,偶数则是后面那个中点 index = 0 status = True while index < mid_index: if s[index] == s[length - 1 - index]: index += 1 else: status = False break return status
有帮助的话采纳一下哦!
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用