如何定义递归样式函数findmax,其工作原理如下:
findmax('abc')-->['abc']
findmax('afz')-->['afz']
findmax('cba')-->['c','b','a']
findmax('zfa')-->['z','f','a']
findmax('abczabc')-->['abcz']
findmax('abcabc')-->['abc','abc']
该函数只显示一个或多个a-z字符。并返回所有按升序排列的最长子字符串。在
我为什么问这个问题?因为我的直觉告诉我一定有一个优雅的回避式的解决方案。但遗憾的是,我无法解决。在
请看看我写的可怕的解决方案:
^{pr2}$
下面是一个递归的解决方案。此函数是纯递归的,没有for循环,更确切地说,根本没有循环:
如果你真的想要一个递归风格的函数。我不知道它是否更优雅,但我知道它比命令式(递归限制和尾部调用)更不有效,也更受限制:
根本不需要递归。在
测试用例:
^{pr2}$Explanation can be found here(对该代码稍作修改)。在
相关问题 更多 >
编程相关推荐