To prove safety offer Q7 Fibonacci number

To prove safety offer Q7

Title:
We all know that Fibonacci number, and now asked to enter an integer n, you output the n-th Fibonacci number Fibonacci sequence of item (from 0, the first 0 is 0). n <= 39

Fibonacci number (Fibonacci sequence), also known as golden columns: 1,1,2,3,5,8,13,21,34, ...... In mathematics, Fibonacci listed in the following are to be handed push method defined: F (1) = 1, F (2) = 1, F (n) = F (n - 1) + F (n - 2) (n ≥ 3, n ∈ N *)

The number of columns from the beginning of paragraph 3, each of which is equal to the sum of the first two.

By direct comparison of recursive time-consuming
the recommended solution to a problem in calling me thinking Pikachu method provides very clear:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

class Solution:
    def Fibonacci(self, n):
        # write code here
        x=0;y=1
        if n ==0:
            return 0
        elif n <=2:
            return 1
        for i in xrange(2,n+1):
            x,y = y,x+y
        return y

注意:
for i in range(0,5):print i #0 1 2 3 4
for i in xrange(0,5):print i #0 1 2 3 4

Published 15 original articles · won praise 0 · Views 124

Guess you like

Origin blog.csdn.net/kikiwindsky/article/details/104223507
Recommended