But that's also nice, it lets you see how the recursive thing is simply unwrapping but the complexity in terms of the amount of time it takes is going to be the same.
它让我们看到了,在复杂度依照时间总数来看,没有变化的情况下,递归是怎么一步步的展开的,我欠你一个糖,谢谢。
So this is very similar, this is a kind of recursive thinking we talked about earlier, where we take our problem and we make it smaller we solve a smaller problem, et cetera.
我们则跳过比猜想数小的那个区间,然后我们重复这一过程,跟之前我们讲过的,递归思想非常类似,我们解决问题的时候,先把问题一步步变小,然后解决小问题。
But I want to stress again, as long as I do the base case right and my inductive or recursive step reduces it to a smaller version of the same problem, the code will in fact converge and give me out an answer.
就开心的去做吧,但是我想再次强调,只要基础事件处理正确而我的递归,或递推步骤能把它简化为更简单的同类问题,那么这段代码就可以收敛。
In the case of a recursive exponentiator, I'm going to do the following trick.
在这个迭代求乘方的例子里面,我会用这么个小把戏。
This is another way of doing exponentiation, but this one's a recursive function. All right?
但这是一种递归的方法对不对?,好,让我们看看这种方法?
Once you hear that description, it's easy to write the code, in fact. This is a place where the recursive version of it is much easier to think about than the iterative one.
实际上一旦你听到了大致描述,就能很轻松的写出代码来,在这一点上应用递归来解决问题,比用迭代要容易理解多了。
That this recursive call had two sub-problems of a smaller size, not one.
而不是一个,这会有很大的不同。
There are other problems that are much more naturally thought of in a recursive fashion.
有一些问题不递归,也可以很简单的解决。
We saw some quadratic algorithms, typically those are things with multiple nested loops, or iterative or recursive calls, where you're doing, say, a linear amount of time but you're doing it a linear number of times and so it becomes quadratic, and you'll see other polynomial kinds of algorithms.
我们看过一些平方算法,他们一般进行了多次嵌套循环,或者递归迭代调用,对一个线性操作调用线性次,这样就变成平方次了,以后你们能看到,一些多项式算法。
It's actually a wonderful recursive definition.
这就是一个绝妙的递归定义。
5 recursive calls to get there.
5次递归调用,才得到这个答案。
fib Roughly speaking, the analysis of fib is actually quite complex, of a recursive fib.
简单的说,递归的fib的分析实际上非常复杂。
I just used a recursive definition.
我需要进行递归定义。
Whether it was recursive or iterative.
但是在这个问题里。
It's a recursive definition.
但是注意。
Wonderful recursive definition.
很漂亮的递归定义。
Nobody in their right mind actually implements a recursive Fibonacci the way I did it originally.
没有谁真的在右脑中,按我最初讲的递归方法,计算Fibonacci数列。
And so we've got it up here, a nice little recursive implementation of it.
然后在这里有个这样的数列,一个很好的用递归来实现的斐波那契i数列。
应用推荐