1807. 斐波纳契数列简单

1807. 斐波纳契数列简单

中文 English

Find the Nth number in Fibonacci sequence.

A Fibonacci sequence is defined as follow:

  • The first two numbers are 0 and 1.
  • The i th number is the sum of i-1 th number and i-2 th number.

The first ten numbers in Fibonacci sequence is:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...

样例

Example 1:
	Input:  1
	Output: 0
	
	Explanation: 
	return the first number in  Fibonacci sequence .

Example 2:
	Input:  2
	Output: 1
	
	Explanation: 
	return the second number in  Fibonacci sequence .

注意事项

N <= 20

输入测试数据 (每行一个参数) 如何理解测试数据?
class Solution:
    """
    @param n: an integer
    @return: an ineger f(n)
    """
    '''
    大致思路:
    1.前两个是固定的[0,1],后面的就根据前面的两个相加,到指定长度返回。
    '''
    def fibonacci(self,n):
        dic=[0,1]
        for i in range(n-2):
            dic.append(dic[-1]+dic[-2])
        return dic[n-1]

猜你喜欢

转载自www.cnblogs.com/yunxintryyoubest/p/12817412.html