标签:
1 class Solution { 2 public: 3 int Fibonacci(int n) { 4 if(n<=1) 5 return n; 6 else 7 return Fibonacci(n-1)+Fibonacci(n-2) ; 8 } 9 };
1 class Solution { 2 public: 3 int Fibonacci(int n) { 4 if(n<=1) 5 { 6 returnn; 7 } 8 int[] record = newint[n+1]; 9 record[0] = 0; 10 record[1] = 1; 11 for(inti=2;i<=n;i++) 12 { 13 record[i] = record[i-1] + record[i-2]; 14 } 15 returnrecord[n]; 16 } 17 };
或者:
1 class Solution { 2 public: 3 int Fibonacci(int n) { 4 int target=0; 5 if(n<=1) 6 return n; 7 int first=0,second=1; 8 for(int i=2;i<=n;i++) 9 { 10 target=first+second; 11 first=second; 12 second=target; 13 } 14 return target; 15 } 16 };
标签:
原文地址:http://www.cnblogs.com/lou424/p/5023843.html