C++
1 class Solution{ 2 public: 3 /** 4 * @param n: an integer 5 * @return an integer f(n) 6 */ 7 int fibonacci(int n) { 8 // write your code here 9 int a=0,b=1; 10 for (int i=1; i<n; i++) { 11 a = a+b; 12 b = a-b; 13 } 14 return a; 15 } 16 };
C++
1 class Solution{ 2 public: 3 /** 4 * @param n: an integer 5 * @return an integer f(n) 6 */ 7 int fibonacci(int n) { 8 // write your code here 9 int a=0,b=1; 10 for (int i=1; i<n; i++) { 11 a = a+b; 12 b = a-b; 13 } 14 return a; 15 } 16 };