In mathematics, the Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers in the following integer sequence: 0, 1, 1, 2,
3, 5, 8, 13, 21, 34, 55, 89, 144... By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.
public static ArrayList<Integer> fibonacci(int num) {
ArrayList<Integer> res = new ArrayList<Integer>();
if (num == 0) {
return res;
}
int first = 0;
int second = 1;
if (num == 1) {
res.add(first);
} else {
res.add(first);
res.add(second);
}
for (int i = 1; i <= num - 2; i++) {
int third = first + second;
res.add(third);
first = second;
second = third;
}
return res;
}