Fibonacci-Is this an algorithm? Love love

Fibonacci sequence (Fibonacci)

Today, I started to be the sword finger of Niu Ke, Offie. As soon as I saw Fibonacci, I thought of recursion, it is it and it is it, and then I wrote the following code with anticipation. ok, no problem, just one time. However, I dropped the old swan and only defeated 30% of the people.

Popular science : Fibonacci sequence (Fibonacci sequence), also known as the golden section sequence. Refers to such a series:

0、1、1、2、3、5、8、13、21、34、……

F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2)

Method 1 (ordinary, I can come up with this color pen all at once)

public class Solution {
    
    
    public int Fibonacci(int n) {
    
    
        if(n <= 1){
    
    
            return n;
        }
        return Fibonacci(n-2)+Fibonacci(n-1);
    }
}
the complexity:

Time complexity: O(2^n)
Space complexity: O(1)

I just want to study the following Sao operations.

Method two is to optimize recursion

After reading it, I can only say that it is absolutely amazing. If n=5, it returns Fibonacci(3)+Fibonacci(4), and Fibonacci(4) recursively returns Fibonacci(2)+Fibonacci(3). Now there are two duplicates of n=3 Calculation, and n=2 has to be repeated calculations, and at this time n is only 5. If it is larger, repeated calculations will consume a lot of resources. Now use the array to save the result.

public class Solution {
    
    
    public int Fibonacci(int n) {
    
    
        int ans[] = new int[40];
        ans[0] = 0;
        ans[1] = 1;
        for(int i=2;i<=n;i++){
    
    
            ans[i] = ans[i-1] + ans[i-2];
        }
        return ans[n];
    }
}
the complexity:

Time complexity: O(n)
Space complexity: O(n)

Because an array is used to store the results, there will be additional costs, but the time complexity is exponentially reduced.

Method three optimize storage

After reading this, I called an expert directly, and found that we only used the two most recent numbers for each calculation, so we only need to store the two most recent numbers. Assuming n=3, we only use the results of n=1 and n=2. When n=4, we only use the results of n=2 and n=3. At this time, we can give up the result of n=1. storage. No matter how big n is, what we want to store each time is only the last two numbers.

  • sum stores the value of the nth item
  • one stores the value of item n-1
  • two stores the value of item n-2
public class Solution {
    
    
    public int Fibonacci(int n) {
    
    
        if(n == 0){
    
    
            return 0;
        }else if(n == 1){
    
    
            return 1;
        }
        int sum = 0;
        int two = 0;
        int one = 1;
        for(int i=2;i<=n;i++){
    
    
            sum = two + one;
            two = one;
            one = sum;
        }
        return sum;
    }
}
the complexity:

Time complexity: O(n)
Space complexity: O(1)

Method four is optimized on the basis of three

After reading it, it is just a word woc. Is this the world of algorithms? Loved love. The big guys figured out that saving only the last two numbers still didn't work, so they pulled out their hair to sacrifice. So there is this idea. Observation method three finds that sum is not f(n-1) before each assignment? Suppose that after calculating f(5), sum saves the value of f(5), then calculate f(6), f(6) = f(5) + f(4). ! ! Do we find that sum stores f(5)? f(4) is stored in one.

public class Solution {
    
    
    public int Fibonacci(int n) {
    
    
        if(n == 0){
    
    
            return 0;
        }else if(n == 1){
    
    
            return 1;
        }
        int sum = 1;
        int one = 0;
        for(int i=2;i<=n;i++){
    
    
            sum = sum + one; 
            one = sum - one; //刷新值,f(n-1)=f(n)-f(n-2)
        }
        return sum;
    }
}
the complexity:

Time complexity: O(n)
Space complexity: O(1)

Guess you like

Origin blog.csdn.net/weixin_43957211/article/details/114585762