回溯N楼梯问题得到0例Java
N 楼梯问题是计算到达顶部的不同方式的数量。每次您可以爬 1 或 2 级台阶。例如,如果输入为 3,则所需输出为 3 (1+1+1,1+2,2+1)。
我正在学习Java中的回溯,所以我想在这个问题中实现它,尽管DP在这种情况下会工作得更好。如果我很好地理解回溯,我会将其视为一种实践,但显然不是。我陷入了困境,因为我的算法为每种情况都提供了 0
输出。下面是我的代码:
public int climbStairs(int n) {
int cnt=0,k=0;
climb(cnt,k,n);
return cnt;
}
public void climb(int cnt, int k, int n){
if(k==n){
cnt++;
return;
}
for(int i=1;i<3;++i){
if(k<n){
k+=i;
climb(cnt,k,n);
k-=i;
}
}
}
你能帮我找出我的代码有什么问题吗?我尝试调试它,我注意到每次返回时,cnt
都会自动更改为0
,但我就是不明白为什么。
提前非常感谢!
编辑版本:
public class ClimbStairs {
public static void main(String[] args) {
System.out.println(climbStairs(3));
}
public static int climbStairs(int n) {
int cnt=0,k=0;
return climb(cnt, k, n);
}
public static int climb(int cnt, int k, int n){
if(k==n){
cnt++;
}
for(int i=1;i<3;++i){
if(k<n){
k+=i;
climb(cnt,k,n);
k-=i;
}
}
return cnt;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每个递归实现都由两部分组成:
k == n
。如果我们不跟踪步数,那么基本情况将用两个边缘情况表示:有根本没有楼梯 - 不需要步骤,并且只有一种方法可以走零步,所以输出是1
,第二种是当我们只有一个楼梯那么我们必须走一个步骤,输出也是1
。对于每个
k <<,在递归情况中,我们有两个可能的分支。 n
:我们可以采取1步或2步。因此,我们必须进行两次递归调用来代表这些情况。所有这些调用都可以以图形方式表示为树。为了更好地理解逻辑,您可以从climbStairs()
内创建的第一个方法调用树开始绘制这个方法调用树,并跟踪k 的值
每次调用。直到条件k < n
是肉,每个顶点会产生两个分支。当k
大于或等于n
时,调用将达到基本情况。从下到上,您可以计算出每次调用的返回值是什么。另请注意,您的方法必须返回找到的路径数,而不是
void
。而代表这个数字的第三个参数是多余的。正如我上面所说,不需要跟踪步数。相反,我们可以在进行递归调用时减少楼梯数量。这将使我们能够将所有逻辑放在单个方法中。就像这样:
输出
Every recursive implementation consists of two parts:
k == n
. And if we don't track the number of steps then the base case will be represented with two edge-cases: there are no stairs at all - no steps required and there's only one way to take zero steps, so the output is1
, and the second is when we have only one stair then we have to take one step and the output is also1
.We have two possible branches in the recursive case for every
k < n
: we can either take 1 step or 2 steps. And therefore we have to make two recursive calls that represent these situations. All these calls can be represented graphically as a tree. To understand the logic better can you drow this tree of method-calls starting from the first one that is made inside theclimbStairs()
and track the value ofk
for each call. Until the conditionk < n
is meat each vertex will spawn two branches. Whenk
is greater or equal ton
the call hits the base case. And starting from the bottom to the top you can figure out what are the return values for each call.Also, note that instead of being
void
your method must return the number of paths that are found. And the third parameter that represents this number is redundant.As I said above tracking the number of steps isn't required. Instead, we can reduce the number of stairs while making recursive calls. And that will allow us to place all logic in a single method. Like that:
Output
Java 是按值传递的。这意味着您的
climbStairs
方法中的cnt
变量未共享;它对你来说是独一无二的。当您调用climb
时,您传递它所保存的值(这里每次都为0
),并且climb
有自己的变量(也称为0
>cnt)。它修改自己的cnt
值,该值在该方法结束时被扔进垃圾箱(当方法结束时,所有参数和所有局部变量总是被扔进垃圾箱)。您想要返回
cnt
:Java is pass-by-value. That means your that your
cnt
variable in yourclimbStairs
method is not shared; it is unique to you. When you invokeclimb
, you pass the value it holds (0
every time here), andclimb
has its own variable (also calledcnt
). It modifies its owncnt
value which is tossed in the garbage when that method ends (all parameters and all local variables are always tossed in the garbage when a method ends).You want to return
cnt
:我们已知有
N
个步骤。令n
等于所采取的两步数 (0 <= n <= N/2
)。如果采取n
两步,则采取N-2*n
一步。当有
n
个两步时,令f(n)
等于一步和两步的组合数量。这只是 多项分布具有两个不同的项:为了获得所需的组合总数,我们只需将
f(n)
与n = 0..N 相加/2
。对于
N = 3
(问题中给出的示例),组合数量计算如下。对于
N = 5
,组合数量计算如下。We are given that there are
N
steps. Letn
equal the number of two-steps taken (0 <= n <= N/2
). Ifn
two-steps are takenN-2*n
one-steps are taken.Let
f(n)
equal the number of combinations of one-steps and two-steps when there aren
two-steps. This is merely a coefficient in a multinomial distribution having two distinct items:To obtain the desired total number of combinations we simply sum
f(n)
overn = 0..N/2
.For
N = 3
(the example given in the question), the number of combinations is computed as follows.For
N = 5
, the number of combinations is computed as follows.