7. recursion and cycles

Dynamic Programming (recursive) problem-solving steps:

  1. The original problem is split into sub-problems.

  2. Confirm state.

  3. Confirm boundary conditions (initial conditions).

  4. The state transition equation.

 

A title: [Fibonacci number]

We all know that Fibonacci number, and now asked to enter an integer n, you output the n-th Fibonacci number Fibonacci sequence of item (from 0, the first 0 is 0). n <= 39

Analysis: Fibonacci number: {0112358132144 ......}

  F(0)=0

  F(1)=1

  F(2)=F(1)+F(0)

  F(3)=F(2)+F(1)

  ...

  F(n)=F(n-1)+F(n-2)

1  public  class solution {
 2      // 递归O (n ^ 2) 
3      public  int fibonacci ( int n) {
 4          if (n == 0 || n == 1) return n ;
5          return fibonacci (n-1) + fibonacci (n-2 );
6      }
 7 }

 

. 1  public  class Solution {
 2      // loop O (N) 
. 3      public  int the Fibonacci ( int n-) {
 . 4          IF (n-n-== == 0 ||. 1) return n-;
 . 5          int First =. 1 ;
 . 6          int SECOND = 0 ;
 . 7          for ( int I = 2; I <= n-; I ++ ) {
 . 8              First = First + SECOND; // F. (n-) = F. (n--. 1) + F. (n--2) 
. 9              SECOND = First - SECOND ; // F. (. 1-n-) = F. (n-) -F (n--2) where the next cycle is used 
10          }
 11         return first;
12     }
13 }

 

 

Question two: [jump] steps

A frog can jump on a Class 1 level, you can also hop on level 2. The frog jumped seeking a total of n grade level how many jumps (the order of different calculation different results).

Analysis: n = 1, only one jumps;

   When n = 2, 1 + 1 and 2, two kinds of jumps;

   when n = 3, the last time when the second step jump jump to only one - a jump, jump to the first time when a method step is only hop - two-hop (and if so a jump repeatedly multiplexing the first case);

   Only one jump when n = n, when the last skip of n-1 th step - a jump, jump to when the last step n-2-th one hop method - two hops;

   Is the Fibonacci number!

   [A] problem with the code

   

Question three: [level] jump metamorphosis

A frog can jump on a Class 1 level, you can also hop on level 2 ...... n It can also jump on stage. The frog jumped seeking a total of n grade level how many jumps

Analysis: When n = n, when there is a jump to the previous n-1 1 method, when the last skip also a n-2, ......, when the last skip n = 1, only a species;

   F(n) = F(n-1)+F(n-2)+F(n-3)+...+F(1)

   F(n-1) = F(n-2)+F(n-3)+F(n-4)+...+F(1)

   =>F(n)-F(n-1)=F(n-1)  =>F(n)=2*F(n-1)

 1 public class Solution {
 2     public int JumpFloorII(int target) {
 3         if(target==0||target==1) return target;
 4         int res=1;
 5         for(int i=2;i<=target;i++){
 6             res = res*2;
 7         }
 8         return res;
 9     }
10 }

 

 

Question four: a rectangular footprint []

We can use a small rectangle 2 * 1 sideways or vertically to cover a larger rectangle. Will the small rectangle of n 2 * 1 coverage without overlap a large rectangle 2 * n, a total of how many ways?

 

  F (n) = F (n-1) + F (n-2) => Fibonacci number

 [A] problem with the code

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/qmillet/p/12021108.html