A recursive algorithm is a special type of algorithm in which a method calls itself over and over again to solve a problem. Each successive call reduces the complexity of the problem and moves closer and closer to a solution until finally a solution is reached, the recursion stops, and the method can exit.
递归算法就是不断调用自己使得复杂问题不断趋近与被解决, 当问题被解决就停止递归。
可以看出递归算法的要素是,
1. 调用自己
2. 每次调用,要趋近问题解决
3. 问题解决退出
Remember, the objective of a recursive algorithm is to break a complex problem into a smaller more manageable problem.
所以,递归算法要够简洁,否则就要重新思考你解决问题的思路。
示例:
Line 3 是work;Line4~7是base cases;line 10是Recursion
或者这样写也是一样的。base cases不如上面的明显,其实一样,number=0也是要退出。
We can still stay, the base case occurs when the number parameter is zero.
Winding is simply the work that occurs before a recursive call, and unwinding is the work that occurs after the recursive call.
比较:
执行CountDown(5),结果是5,4,3,2,1,0;
执行CountUp(5), 结果是0,1,2,3,4,5;
记住, the location of “the work” in a recursive algorithm can affect the timing of when it is executed.
In attempt to make writing recusive algorithms a bit more approachable, I’ve broken the process down into the following six steps:
Problem:
0! = 1
n! = n * (n-1)!
Factorial sounds like a great name for the method. Second, we know that the factorial function accepts a single integer as an input, and returns and integer value. As such, let’s go ahead and create the following method signature:
public int Factorial(int n)
{
}
Next, we need to incorporate the base case into the method. Look back at the mathematical definition for factorials. There are two definitions. One uses recursion. One does not. Guess which one is the base case? If you guessed the first one, then you are right.
public int Factorial(int n)
{
if(n==0) return 1;
}
Look problem, you will see that the recursive part of the algorithm has been laid out for us fairly well. It’s simply a matter of getting the equation translated into code.
01 public int Factorial(int n)
02 {
03 if(n==0) return 1;
04 return n * Factorial(n-1);
05 }
Now we have a functioning, recursive Factorial function. Fortunately, most interview questions involving recursion center on mathematical concepts like Factorials and Fibonacci numbers.
01 public UInt64 Factorial(byte n)
02 {
03 return n==0 ? 1 : n * Factorial(n-1);
04 }
a few key differences: the body is on a single line,
1. it makes use of the conditional operator instead of an if statement,
2. it uses an unsigned long integer as the return value
3. and a byte as the input parameter instead of integers.
使用int作为返回值, the method will crap out at Factorial(17) because the result is larger than an integer.
A long value gets you all the way to Factorial(20).
And the Uint64 allows you to go all the way up to Factorial(65) before you encounter issues.
Since the method quits working when you pass in 65, an int value is overkill. A short value is overkill too. So you use a byte.
Pseudocode
function fib is:
input: integer n such that n >= 0
1. if n is 0, return 0
2. if n is 1, return 1
3. otherwise, return [ fib(n-1) + fib(n-2) ]
end fib
Refer to:http://blog.chinaunix.net/u/16292/showart_496723.html
递归的方法,时间复杂度O(N^2); 递推的方法,时间复杂度O(N)
这个问题有点变态,不容易轻易理解解决。
问题是:10枪打90环,求所有可能组合?
Ref: http://www.cnblogs.com/lds85930/archive/2007/07/05/807198.html
DataStructure_Algorithm/Recursive/