7、斐波那契数列
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0,第1项是1)。
n<=39
===================Python================
# -*- coding:utf-8 -*- class Solution: def Fibonacci(self, n): # write code here a, b = 0, 1 if n == 0: return 0 for i in range(1, n+1): a, b = b, a+b return a
======================Java=====================
public class Solution { public int Fibonacci(int n) { int a = 0; int b = 1; if (n == 0) return 0; for (int i = 1; i <= n; i++){ int tmp = a; a = b; b = tmp + b; } return a; } }