Fibonacci columns and frog jump to step problem

Fibonacci number

Here Insert Picture Description
Two ideas

  • Iteration
class Solution {
	int start1=1,start2=1,t=0;
    public int numWays(int n) 
    {
     	for(int i=1,i<=n;i++)
     	{
     		   t=start1+start2;
     		   start1=start2
     		   start2=t;
  		}		   
    }
}
  • Recursion
public int fib(int n)
	{
		if(n==0)
			return 0;
		else if(n==1||n==2)
			return 1;
		else
			return fib(n-1)+fib(n-2);
	}

Frog jump stairs

Letcode
Man of few words said on the map analysis:
Here Insert Picture Description
frog jump from level 0 when layer 0, 0
frog jump from level 0 when the first layer is 1, because only the first layer from 0 jump step to reach the first layer
when it frog to reach the n-layer, there are two ways:

  • From the n-1 layer jump step

  • From the n-2 layer jump-step

According to the permutations and combinations of knowledge, this process for a classification process:
i.e., f (n) = f (n -1) + f (n-2), where f (n) represents from layer 0 to large n-th layer moves
you can see the solution process is actually a Fibonacci number

Published 16 original articles · won praise 1 · views 1258

Guess you like

Origin blog.csdn.net/cf1169983240/article/details/104362394