longfab2(int n) { long nums[n]; nums[0] = 1; nums[1] = 1; for (int i = 2; i < n; i++) { nums[i] = nums[i - 2] + nums[i - 1]; } return nums[n - 1]; }
「滚动数组」实现
1 2 3 4 5 6 7 8 9 10 11 12 13
longfab3(int n) { long first = 1; long second = 1; long current = 1; for (int i = 3; i <= n; i++) { current = first + second; first = second; second = current; } return current; }