Merge f21fa6cd8ef0b262388d5479bfb272da5d14e869 into b70121d377cb6005eb65f12b098cd5decd905669

This commit is contained in:
chandan23sde 2023-07-20 02:07:08 -07:00 committed by GitHub
commit 52dc0f2fb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,6 +31,22 @@ public int Fibonacci(int n) {
return fib[n]; return fib[n];
} }
``` ```
OR Simplified code
class FibonacciExample1{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1
for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}}
考虑到第 i 项只与第 i-1 和第 i-2 项有关,因此只需要存储前两项的值就能求解第 i 项,从而将空间复杂度由 O(N) 降低为 O(1)。 考虑到第 i 项只与第 i-1 和第 i-2 项有关,因此只需要存储前两项的值就能求解第 i 项,从而将空间复杂度由 O(N) 降低为 O(1)。