6, the sword refers to the offer of the Fibonacci sequence, topic resolution and Java implementation method

Source: Internet
Author: User
Topic description

We all know the Fibonacci sequence, and now you're going to need to enter an integer n, so you output the nth item in the Fibonacci sequence. n<=39 Requirements

Time limit: 1 seconds space limit: 32768K knowledge Point

Fibonacci Series: 1,1,2,3,5,8,13 ....
Observations found: F (1) = 1 F (2) =1 f (3) = f (2) +f (1) = 2 F (4) =f (3) +f (2) ...

F (n) = f (n-1) +f (n-2)

The first item of the series is equal to 1, the second item of the sequence equals 2, and when N>2, the latter is equal to the sum of the first two. Code Implementation

    public int Fibonacci (int n) {
        if (n==0) {return
            0;
        }
        int[] array = new int[39];
        Array[0]=1;
        Array[1]=1;
        for (int i=2;i<n;i++) {
            array[i]=array[i-1]+array[i-2];
            System.out.print ("" "+array[i]);
        }
        return array[n-1];
    }

Welcome you to pay attention to my sword refers to the offer blog column, not regularly updated blog content, the wrong place welcome everyone to come forward and learn together.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.