给定一个数字N ,我们需要找到直到N项的斐波那契数列。
The Fibonacci series is a series of elements where, the previous two elements are added to get the next element, starting with 0 and 1.
例子:
Input: N = 10
Output: 0 1 1 2 3 5 8 13 21 34
Here first term of Fibonacci is 0 and second is 1, so that 3rd term = first(o) + second(1) etc and so on.
Input: N = 15
Output: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
方法 1 – 迭代:将第一个和第二个数字初始化为0 和 1 。在此之后,我们打印第一个和第二个数字。然后我们将流发送到迭代 while 循环,在那里我们通过将前两个数字相加得到下一个数字,同时我们将第一个数字与第二个数字交换,将第二个数字与第三个数字交换。
下面是上述方法的实现:
Java
// Java program for the above approach
class GFG {
// Function to print N Fibonacci Number
static void Fibonacci(int N)
{
int num1 = 0, num2 = 1;
int counter = 0;
// Iterate till counter is N
while (counter < N) {
// Print the number
System.out.print(num1 + " ");
// Swap
int num3 = num2 + num1;
num1 = num2;
num2 = num3;
counter = counter + 1;
}
}
// Driver Code
public static void main(String args[])
{
// Given Number N
int N = 10;
// Function Call
Fibonacci(N);
}
}
Java
// Recursive implementation of
// Fibonacci Series
class GFG {
// Function to print the fibonacci series
static int fib(int n)
{
// Base Case
if (n <= 1)
return n;
// Recursive call
return fib(n - 1)
+ fib(n - 2);
}
// Driver Code
public static void
main(String args[])
{
// Given Number N
int N = 10;
// Print the first N numbers
for (int i = 0; i < N; i++) {
System.out.print(fib(i) + " ");
}
}
}
Java
// Dynamic Programming approach for
// Fibonacci Series
class fibonacci {
// Function to find the fibonacci Series
static int fib(int n)
{
// Declare an array to store
// Fibonacci numbers.
// 1 extra to handle case, n = 0
int f[] = new int[n + 2];
int i;
// 0th and 1st number of
// the series are 0 and 1
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++) {
// Add the previous 2 numbers
// in the series and store it
f[i] = f[i - 1] + f[i - 2];
}
// Nth Fibonacci Number
return f[n];
}
public static void
main(String args[])
{
// Given Number N
int N = 10;
// Print first 10 term
for (int i = 0; i < N; i++)
System.out.print(fib(i) + " ");
}
}
0 1 1 2 3 5 8 13 21 34
时间复杂度: O(N)
辅助空间: O(1)
方法 2 – 使用递归:因为斐波那契数是前两个数的和。我们可以根据以下条件使用递归:
- 获取需要计算其斐波那契数列的数字。
- 递归地从值 N 迭代到 1:
- 基本情况:如果递归调用的值小于 1,则返回 1函数。
- 递归调用:如果不满足基本情况,则递归调用前两个值:
recursive_function(N – 1) + recursive_function(N – 2);
- 返回语句:在每次递归调用时(基本情况除外),将前两个值的递归函数返回为:
recursive_function(N – 1) + recursive_function(N – 2);
下面是上述方法的实现:
Java
// Recursive implementation of
// Fibonacci Series
class GFG {
// Function to print the fibonacci series
static int fib(int n)
{
// Base Case
if (n <= 1)
return n;
// Recursive call
return fib(n - 1)
+ fib(n - 2);
}
// Driver Code
public static void
main(String args[])
{
// Given Number N
int N = 10;
// Print the first N numbers
for (int i = 0; i < N; i++) {
System.out.print(fib(i) + " ");
}
}
}
0 1 1 2 3 5 8 13 21 34
时间复杂度: O(2 N )
辅助空间: O(1)
方法 3 – 使用动态规划:我们可以通过存储到目前为止计算的斐波那契数来避免方法 2 中所做的重复工作。以下是步骤:
- 创建一个大小为N的数组arr[] 。
- 初始化 arr[0] = 0,arr[1] = 1。
- 迭代[2, N]并将数组arr[]更新为:
arr[i] = arr[i – 2] + arr[i – 1]
- 打印arr[N]的值。
下面是上述方法的实现:
Java
// Dynamic Programming approach for
// Fibonacci Series
class fibonacci {
// Function to find the fibonacci Series
static int fib(int n)
{
// Declare an array to store
// Fibonacci numbers.
// 1 extra to handle case, n = 0
int f[] = new int[n + 2];
int i;
// 0th and 1st number of
// the series are 0 and 1
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++) {
// Add the previous 2 numbers
// in the series and store it
f[i] = f[i - 1] + f[i - 2];
}
// Nth Fibonacci Number
return f[n];
}
public static void
main(String args[])
{
// Given Number N
int N = 10;
// Print first 10 term
for (int i = 0; i < N; i++)
System.out.print(fib(i) + " ");
}
}
0 1 1 2 3 5 8 13 21 34
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。