Java 关于递归的几个问题
什么是直接递归?什么是无限递归 基本情况是什么?为什么有必要/重要 示例代码:Java 关于递归的几个问题,java,recursion,Java,Recursion,什么是直接递归?什么是无限递归 基本情况是什么?为什么有必要/重要 示例代码: public static void indifferent(int x, int y) { if (x <= y) {// base case System.out.print("!"); }else { System.out.print(x); indifferent(x - 1, y + 2); System.out.p
public static void indifferent(int x, int y) {
if (x <= y) {// base case
System.out.print("!");
}else {
System.out.print(x);
indifferent(x - 1, y + 2);
System.out.print(y);
}
}
公共静态无效(int x,int y){
if(x直接递归:方法调用自身
间接递归:方法调用其他方法,多次调用后,调用返回到第一个调用方法再次被调用的情况
基本情况:它是任何递归函数的一个重要组成部分。它是递归方法停止调用自身从而结束递归的一个条件,即停止深入递归。基本上,如果你认为递归是一个yoyo,那么当递归处于其最末端时,它就是基本情况
无限递归:它是一个永远不会结束的递归。基本上它是一个没有基本情况的递归函数。(一个不会回来到处跑的悠悠球。)
在这里签出这些有趣的递归图像,直接递归:方法调用自身
间接递归:方法调用其他方法,多次调用后,调用返回到第一个调用方法再次被调用的情况
基本情况:它是任何递归函数的一个重要组成部分。它是递归方法停止调用自身从而结束递归的一个条件,即停止深入递归。基本上,如果你认为递归是一个yoyo,那么当递归处于其最末端时,它就是基本情况
无限递归:它是一个永远不会结束的递归。基本上它是一个没有基本情况的递归函数。(一个不会回来到处跑的悠悠球。)
在这里签出这些有趣的递归图像
直接递归是当方法调用自身时(如示例代码中所示)。间接递归是当方法a()
调用方法b()
,方法本身(直接或间接)再次调用方法a()
无限递归是永不结束的递归。另一种说法是无限递归是当基本情况不存在或从未遇到时
基本情况是一组参数值,它使方法返回而不需要递归调用(直接调用或间接调用)
直接递归是当方法调用自身时(如示例代码中所示)。间接递归是当方法a()
调用方法b()
,方法本身(直接或间接)再次调用方法a()
无限递归是永不结束的递归。另一种说法是无限递归是当基本情况不存在或从未遇到时
基本情况是一组参数值,它使方法返回而不需要递归调用(直接调用或间接调用)
这是一个爬楼梯的算法
climb:
if you are at the top
stop
otherwise
step up and then climb
这显示了直接递归(爬升是爬升中的一步)和基本情况(如果在顶部,则停止)
无限递归的两种情况:
climb:
step up and then climb
climb:
if you are at the top
stop
otherwise
step up and then step down and then climb
group climb:
if there's no one in the group
stop
otherwise
step up
step up:
if first in group is at top
remove them from the group then group climb
otherwise
step up first in group then group climb
第一个没有基本情况,第二个永远不会到达基本情况:
最后,间接递归:
climb:
step up and then climb
climb:
if you are at the top
stop
otherwise
step up and then step down and then climb
group climb:
if there's no one in the group
stop
otherwise
step up
step up:
if first in group is at top
remove them from the group then group climb
otherwise
step up first in group then group climb
这是一个爬楼梯的算法
climb:
if you are at the top
stop
otherwise
step up and then climb
这显示了直接递归(爬升是爬升中的一步)和基本情况(如果在顶部,则停止)
无限递归的两种情况:
climb:
step up and then climb
climb:
if you are at the top
stop
otherwise
step up and then step down and then climb
group climb:
if there's no one in the group
stop
otherwise
step up
step up:
if first in group is at top
remove them from the group then group climb
otherwise
step up first in group then group climb
第一个没有基本情况,第二个永远不会到达基本情况:
最后,间接递归:
climb:
step up and then climb
climb:
if you are at the top
stop
otherwise
step up and then step down and then climb
group climb:
if there's no one in the group
stop
otherwise
step up
step up:
if first in group is at top
remove them from the group then group climb
otherwise
step up first in group then group climb
让我解释无限递归。请看第一句。让我们添加一个带有基本情况的示例;洗发水说明已读;起泡、冲洗、重复(如有必要)。让我解释无限递归。请看第一句。让我们添加一个带有基本情况的示例;洗发水说明已读;起泡、冲洗、重复(如有必要)。