Topic description
A frog can jump up to 1 steps at a time, or jump up to level 2. How many jumps are there in total for the frog to jump up the N-level steps?
Analysis:
This is a certain rule, we can analyze
First step: F (1) =1
Two steps: F (2) =2
Three steps: F (3) =3
Level Four steps: F (4) =5
Level five steps: F (5) =8
It can be concluded that from the third, the number equals the first two numbers and f (n) = f (n-1) + f (n-2)
public class Solution {
public int Jumpfloor (int target) {
if (target = = 1) {
return 1;
else if (target = = 2) {
return 2;
} else {
Return Jumpfloor (target-1) + Jumpfloor (target-2);
}
}
public static void Main (string[] args) {
Solution s = new Solution ();
System.out.println (S.jumpfloor (6));
}
}
The second method:
public class Solution1 {
public int Jumpfloor (int target) {
int one = 1;
int two = 2;
if (target = = 1) {
return 1;
}
if (target = = 2) {
return 2;
}
int result = 0;
for (int i = 3;i <= target;i++) {
result = one + two;
one = two;
two = result;
}
return result;
}
public static void Main (string[] args) {
Solution1 s = new Solution1 ();
System.out.println (S.jumpfloor (6));
}
}