The sword refers to the offer--9. Fibonacci sequence

Recursion is to call the function itself inside a function, while loop is to repeat the operation in a range by setting the initial value and termination condition of the calculation.

The recursive code is relatively simple, but it also has disadvantages, because function calls are time and space-consuming: each function call needs to allocate space in memory to save parameters, return addresses and temporary variables, and push them into the stack. Data and popping data both take time and may overflow the call stack.

Topic 1: Write a function, enter n, and find the nth term of the Fibonacci sequence. The Fibonacci sequence is defined as follows

f(n)=0,n=0

f(n)=1,n=1

f(n)=f(n-1)+f(n-2),n>1

Idea: recursion can be used, but it is not applicable when n is very large, use loop to solve

public class wr9Fibonacci {
// recurse
	public static long fibonacci(int n){
		if(n<=0){
			return 0;
		}
		if(n==1){
			return 1;
		}
		return fibonacci(n-1)+fibonacci(n-2);
	}
// loop, time complexity is O(n)
	public static long fibonacci2(int n){
		long fn1=1;
		long fn2=1;
		if(n<=0){
			return 0;
		}
		if(n==1 || n==2){
			return 1;
		}
		for(;n>2;n--){
			long temp=fn1;
			fn1=fn1+fn2;
			fn2=temp;
// do not set temporary variables
// fn1=fn1+fn2;
// fn2=fn1-fn2;
		}
		return fn1;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(fibonacci(10));
		System.out.println(fibonacci2(10));

	}

}

Question 2: A frog can jump up 1 step or 2 steps at a time. Find the total number of jumping methods that the frog can jump up a n steps.

Ideas: If there is only one level, there is only one jumping method; if it is level 2, there are two jumping methods, that is, one jump for two levels, and two jumps for each jump of one level. After analysis, it is found that it is actually a Fibonacci sequence.

f(n)=1,n=1

f(n)=2,n=2

f(n)=f(n-1)+f(n-2),n>2
public static int jumpFloor(int target){
		int fn2=1;
		int fn1=2;
		if(target<=0){
			return 0;
		}
		if(target==1){
			return 1;
		}
		if(target==2){
			return 2;
		}
		for(;target>2;target--){
			fn1=fn1+fn2;
			fn2=fn1-fn2;
		}
		return fn1;
	}

Question 3: A frog can jump up to 1 level at a time, or up to 2 levels. It can also jump up to n levels. Find the total number of jumping methods for the frog to jump up an n-level stairs.

Ideas: n steps, the first step has n jumps: jump 1, jump 2,,, jump n steps,
jump 1, and there are n-1 levels left, then the remaining jumping method is f(n-1 )
jumps 2 levels, leaving n-2 levels, then the remaining jump method is f(n-2),
so f(n)=f(n-1)+f(n-2)+...+f( 1)
Because f(n-1)=f(n-2)+f(n-3)+...+f(1)

So f(n)=2*f(n-1)

So it's still the Fibonacci sequence.

f(n)=0,n=0

f(n)=1,n=1

f(n)=2*f(n-1),n>=2
public int jumpFloorMore(int target){
		if(target<=0){
			return 0;
		}
		int count=1;
		for(;target>1;target--){
			count=count*2;
		}
		return count;
	}
Mathematical induction can also be used to prove the calculation formula, which can be substituted into the direct calculation
f(n)=2^(n-1)

Extension: We can use 2*1 small rectangles to cover larger rectangles horizontally or vertically. Q: How many ways are there to cover a 2*8 large rectangle with 8 2*1 small rectangles without overlapping?

Idea: The 2*8 coverage method is recorded as f(8). There are two options to use the first 2*1 small rectangle to cover the leftmost side of the large rectangle, vertical or horizontal. When vertical, the right method is f(7), when horizontal, the right is f(6), so f(8)=f(7)+f(6)

public static int juxing(int target){
		if(target<=0){
			return 0;
		}
		if(target==1){
			return 1;
		}
		if(target==2){
			return 2;
		}
		int fn1=1;
		int fn2=2;
		for(;target>2;target--){
			fn2=fn2+fn1;
			fn1=fn2-fn1;
		}
		return fn2;
	}
Summary: When encountering a problem, sometimes we can find out the law through mathematical logic derivation, we must be good at discovering and thinking

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325608453&siteId=291194637