思路:该题有两种方法。
一种是递归的
一种是非递归的,
代码:
class Solution { public: int Fibonacci(int n) { if(n==0) return 0; if(n==1) return 1; int num_first=0; int num_second=1; int temp; if(n>=2) { for(int i=2;i<=n;i++) { temp = num_first + num_second; num_first = num_second; num_second = temp; } } return temp; /* //没有弄懂自己哪里错了 if(n>=2) { for(int i=2;i<n;i++) { a[i]=a[i-1]+a[i-2]; } } */ } };
原文:https://www.cnblogs.com/loyolh/p/12868776.html