什么是直接递归?什么是无限递归?
什么是基本案例,为什么它是必要的/重要的?
示例代码:
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);
}
}发布于 2016-03-18 01:51:01
a()调用方法b()时,方法本身(直接或间接)再次调用方法a()。发布于 2016-03-18 01:50:19
直接递归:方法调用自身。
间接递归:方法调用其他方法,在多次调用之后,调用返回到第一个调用方法再次被调用的情况。
大小写:它是任何递归函数的重要组成部分。它是递归方法停止调用自身从而结束递归的一个条件,即不再深入递归。基本上,如果您认为递归是一个yoyo,那么当递归处于其极端结束时,它就是基本情况。
无限递归:它是一种永不结束的递归。基本上,它是一个没有大小写的递归函数。(一个不会回来的溜溜球,到处都是。)
在这个链接上查看这些有趣的递归图像
发布于 2016-03-18 02:19:33
这是一种爬楼梯的算法
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 climbhttps://stackoverflow.com/questions/36074670
复制相似问题