Debugging for half a day tips
java.lang.NoSuchMethodError: main
Exception in thread "main"
The program I wrote is below. Is there any obvious mistake?
public class Solution {
public int JumpFloor(int target) {
if (target <= 0) {
return -1;
} else if (target == 1) {
return 1;
} else {
return 2 * JumpFloor(target - 1);
bracket
bracket
public static void main(String[] args) {
Solution st = new Solution();
st.JumpFloor(3);
bracket
bracket
In fact, it is the question of Fibonacci number.
Suppose f(n) is the number of n steps.
f(1) = 1
F(2) will have two ways to jump, first order or second order, which returns to the problem f(1),f(2) = f(2-1) plus f(2-2)
F(3) can jump in three ways, first order, second order and third order, then it is left after the first jump out of first order: f(3-1); Jump out of 2nd order for the first time, leaving F (3-2); For the first third order, then f(3-3) remains. So the conclusion is
F(3) = f(3-1) plus f(3-2) plus f(3-3)When f(n), there will be a jump in n, order 1, order 2 … order n, and the conclusion is as follows:
F(n) = f(n-1) plus f(n-2) plus … plus f(n-(n-1)) plus f(n-n) => f(0) plus f(1) plus f(2) plus f(3) plus … plus f(n-1) == f(n) = 2*f(n-1)
Therefore, it can be concluded that
Attached code
public long jumpFloor(int n) { if (n <= 0) return -1; if (n == 1) return 1; return 2 * jumpFloor(n - 1); bracket
Considering the efficiency, it can also be done by iteration.