递归算法和非递归算法_算法问题解决—递归基础

递归算法和非递归算法

We are going to talk about how to intuitively think about recursion.

我们将讨论如何直观地思考递归。

Problem StatementYou are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 step(s). In how many distinct ways you can climb to the top?

问题陈述 您正在爬楼梯。 它需要n步才能到达顶部。 每次您可以爬1或2步。 您可以通过几种不同的方式攀登顶峰?

This problem is a typical model for recursion. To easily visualize how you can solve this problem assume that you are on the top position already, A friend of yours is going to climb up the stairs.

此问题是递归的典型模型。 为了轻松可视化如何解决此问题,请假设您已经处于最高位置,您的一个朋友将爬上楼梯。

As you started observing this you would have probably identified the pattern, your friend can make the decision of taking either 1 step or 2 steps from the current position. Assume your friend is in Stair #X and if he/she decides to take 1 step from there that will be one distinct route and if he/she decides to take 2 steps from there that will be another distinct route. The problem states asks us to account for all the routes and thus we should basically do this at every step.

开始观察时,您可能已经确定了模式,您的朋友可以决定从当前位置走1步或2步。 假设您的朋友在X楼梯上,并且如果他/她决定从那里走1步,那将是一条不同的路线;如果他/她决定从那里走2步,那将是另一条不同的路线。 问题状态要求我们考虑所有路线,因此我们基本上应该在每个步骤中都这样做。

To add more visualization to this problem, assume that your friend has 5 steps left to climb. Let’s see the number of ways in which he/she can do it.

要为该问题添加更多的可视化效果,请假设您的朋友还有5个要爬的步骤。 让我们看看他/她可以做到的多种方式。

I have highlighted one of the distinct ways to get to the top where the friend took the combination of 2+2+1 to reach the top, the other paths are intentionally left incomplete, but you can very well observe that decision at every point will uniquely generate and path.

我已经强调了一种到达顶峰的独特方法,其中朋友采用2 + 2 + 1的组合达到顶峰,其他路径故意不完整,但是您可以很好地观察到在每个点上的决定都会唯一地生成和路径。

Implementation:

实现方式:

I have added conditions in the code to handle the edge cases in an elegant way, I would highly encourage as an exercise to walk through the example to understand why the conditions work the way they do.

我在代码中添加了条件,以一种优雅的方式处理边缘情况,因此,我强烈建议您练习一下示例,以了解条件为何按其方式工作。

Optimization:Clearly we are doing some duplicate calculations here as you see in the recursion tree the number 3,2, 1 are repeated multiple times and it can be resolved using a technique called Memoization. My next post would be to adding optimization to this problem.

优化:显然,我们在这里进行了一些重复的计算,正如您在递归树中看到的那样,将数字3,2,1重复多次,可以使用一种称为“ 记忆化”的技术对其进行解析 我的下一篇文章是为这个问题添加优化。

Additional problem:Try applying the thinking to the following problem.

附加问题:尝试将思想应用于以下问题。

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

斐波那契数 ,通常表示为 F(n) 形成的序列,称为 Fibonacci序列 ,使得每个数字是两个前述者的总和,从开始 0 1 那是,

F(0) = 0,   F(1) = 1
F(N) = F(N - 1) + F(N - 2), for N > 1.

Given N, calculate F(N).

给定 N ,计算 F(N)

翻译自: https://medium.com/@prashanthps/algorithm-problem-solving-recursion-basics-895430902c9b

递归算法和非递归算法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值