计算爬楼梯方式的递归解决方案
我试图用递归解决“计算到达楼梯第 n 级台阶的方法”的问题。当给定多个要爬的楼梯时,我必须计算一次爬 1 步或 2 步的攀登方式的数量。例如,如果有 4 个楼梯,我们将返回 5,因为我们会:
* 1 1 1 1
* 1 1 2
* 1 2 1
* 2 1 1
* 2 2
我的代码当前抛出堆栈溢出异常:
public static int countWaysToClimb(int stairs) {
return countWaysToClimbHelper(stairs, 0, 0);
}
public static int countWaysToClimbHelper(int sumNeeded, int currentSum, int possibleCombos) {
// base - we will reach this base multiple times
if (sumNeeded == currentSum) {
possibleCombos++;
// if we already found a combo, we need to reset the sum
countWaysToClimbHelper(sumNeeded,0,possibleCombos);
}
else if (currentSum > sumNeeded) {
return 0;
}
// recurse - add 1 and then add 2
countWaysToClimbHelper(sumNeeded,currentSum+1,possibleCombos);
countWaysToClimbHelper(sumNeeded,currentSum+2,possibleCombos);
return possibleCombos;
}
谢谢!
I'm trying to solve the problem of "count ways to reach the nth step in a staircase" with recursion. When given a number of stairs to climb, I have to calculate the number of ways to climb taking either 1 or 2 steps at a time. For example, if there are 4 stairs, we would return 5 since we would have:
* 1 1 1 1
* 1 1 2
* 1 2 1
* 2 1 1
* 2 2
My code is currently throwing a stack overflow exception:
public static int countWaysToClimb(int stairs) {
return countWaysToClimbHelper(stairs, 0, 0);
}
public static int countWaysToClimbHelper(int sumNeeded, int currentSum, int possibleCombos) {
// base - we will reach this base multiple times
if (sumNeeded == currentSum) {
possibleCombos++;
// if we already found a combo, we need to reset the sum
countWaysToClimbHelper(sumNeeded,0,possibleCombos);
}
else if (currentSum > sumNeeded) {
return 0;
}
// recurse - add 1 and then add 2
countWaysToClimbHelper(sumNeeded,currentSum+1,possibleCombos);
countWaysToClimbHelper(sumNeeded,currentSum+2,possibleCombos);
return possibleCombos;
}
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中存在一些问题:
if (sumNeeded == currentSum)
is eat 时生成新分支,而不是返回组合数。您创建了无限递归,这不可避免地会导致StackOverflowError
。您必须在代码中第一个if
之后的大括号内放置 return 语句。并注释掉第一个递归调用(将0
sum 作为参数传递),您将面临第二个问题:对于任何输入,您的代码将产生0
。countWaysToClimbHelper()
的递归调用返回的结果将被忽略。变量possibleCombos
不受这些调用的影响。每个方法调用都会在堆栈(JVM 为每个方法调用存储数据的内存区域)上分配该变量possibleCombos
的自己的副本,并且它们的值无论如何都不相关。在进一步讨论之前,让我回顾一下递归的基础知识。
每个递归方法都应包含两部分:
sumNeeded == currentSum
- 返回值为1
,即找到一个组合;所需总和> currentSum
- 返回值为0
。因此,固定代码可能如下所示:
注意:
countWaysToClimb()
内部实现,而无需使用辅助方法。为此,您需要在递归调用该方法时从sumNeeded
中减去步数,而不是跟踪currentSum
。There are some issues in your code:
if (sumNeeded == currentSum)
is meat instead of returning the number of combinations. You created an infinite recursion that inevitably leads to aStackOverflowError
. You have to place a return statement inside the curly braces after the firstif
in your code. And comment out the first recursive call (with0
sum passed as an argument) you'll face the second problem: for any input, your code will yield0
.countWaysToClimbHelper()
are omitted. VariablepossibleCombos
isn't affected by these calls. Each method call allocates its own copy of this variablepossibleCombos
on the stack (a memory aria where JVM stores data for each method call), and their values are not related anyhow.Before moving further, let me recap the basics of recursion.
Every recursive method should contain two parts:
sumNeeded == currentSum
- the return value is1
, i.e. one combination was found;sumNeeded > currentSum
- the return value is0
.So the fixed code might look like that:
Note:
countWaysToClimb()
without using a helper-method. For that, instead of tracking thecurrentSum
you need to subtract the number of steps from thesumNeeded
when the method is called recursively.