Java Metamorphic Step Implementation Ideas and Codes

  • 2021-06-28 12:25:20
  • OfStack

Metamorphic Step Jump

1. Title Description

A frog can jump up one step or two steps at a time... It can also jump up n.Find out how many jumps the frog can make up an n step.

2. Topic Analysis

f(1) = 1 f (2) has two ways of jumping, first or second order, which returns to the question f (1). f(2) = f(2-1) + f(2-2) f (3) has three jumping modes: first, second, and third order, so it is left after the first jump out of the first order: f (3-1);Jump out of the second order for the first time, leaving f (3-2);The first third order leaves f (3-3). So the conclusion is: f(3) = f(3-1)+f(3-2)+f(3-3) When f (n), there will be n jump mode, first, second and n order, and conclude:

f(n) = f(n-1)+f(n-2)+...+f(n-(n-1)) + f(n-n) = > f(0) + f(1) + f(2) + f(3) + ... + f(n-1) == f(n) = 2*f(n-1)

3. Solution code


public class Solution { 
  public int JumpFloor(int target) { 
    if(target==0){ 
      return 0;   
    } 
    if(target==1){ 
      return 1; 
    } 
    return 2 * JumpFloor(target-1); 
  } 
}

summary


Related articles: