【发布时间】:2011-07-28 19:43:28
【问题描述】:
我想在 python 中使用递归来反转字符串,以便它向后显示字符(即“Hello”将变为“olleh”/“o l l e h”。
我写了一个迭代的方法:
def Reverse( s ):
result = ""
n = 0
start = 0
while ( s[n:] != "" ):
while ( s[n:] != "" and s[n] != ' ' ):
n = n + 1
result = s[ start: n ] + " " + result
start = n
return result
但是我究竟如何递归地做到这一点?我对这部分感到困惑,特别是因为我不经常使用 python 和递归。
任何帮助将不胜感激。
【问题讨论】: