澄清尾部递归方法
以下方法是尾巴恢复性吗?
我相信它不是尾随的递归,因为它依赖于先前的结果,因此需要一个堆栈框架,我是否正确地说明这一点?
public int[] fib(int n)
{
if(n <= 1){
return (new int[]{n,0});
}
else{
int[] F = fib(n-1);
return (new int[]{F[0]+ F[1], F[0]});
}
}
Is the following method tail-recursive?
I believe that it is not tail recursive because it relies on the previous results and so needs a stack frame, am I correct to state this?
public int[] fib(int n)
{
if(n <= 1){
return (new int[]{n,0});
}
else{
int[] F = fib(n-1);
return (new int[]{F[0]+ F[1], F[0]});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是正确的:这不是尾部递归,因为最后一行不是形式
You are correct: It is not tail recursive because the last line is not of the form
是的,您是正确的,因为它不会以将
其转换为尾部回复版本的形式而结束,因此您可以执行
从 https://www.geeksforgeeks.org/tail-recursion-fibonacci/
Yes, you are correct, since it does not end with a call to itself of the form of
To convert it into a tail-recursive version you could do something like
Code taken from https://www.geeksforgeeks.org/tail-recursion-fibonacci/