From 6fe9d53cfd1dfe9c75fe9f81c7bd8f047048b1b0 Mon Sep 17 00:00:00 2001 From: miaod Date: Fri, 10 Mar 2023 16:16:10 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=8F=90=E4=BA=A4=E6=96=90=E6=B3=A2=E9=82=A3?= =?UTF-8?q?=E5=A5=91=E6=95=B0=E5=88=97=E7=AE=97=E6=B3=95=E7=9A=84Java?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=9B=B4=E6=96=B0=20,=20=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E7=9A=84fib(n=20-=202)=20+=20fib=20(n=20-=201)=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E6=97=B6=E9=97=B4=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E8=BF=87=E5=A4=A7=20,=20=E6=97=A0=E6=B3=95=E6=8E=A5=E6=94=B6;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1.1 Fibonacci Implementation/README.md | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/[1]. Math Implementation/1.1 Fibonacci Implementation/README.md b/[1]. Math Implementation/1.1 Fibonacci Implementation/README.md index a011151..5b09070 100644 --- a/[1]. Math Implementation/1.1 Fibonacci Implementation/README.md +++ b/[1]. Math Implementation/1.1 Fibonacci Implementation/README.md @@ -49,20 +49,21 @@ long long Fibonacci(unsigned n) ### Java ```java -public static int Fibonacci( int n ){ - - if( n==0 || n==1 ){ - return 1; - } - else if(n>1){ - - return Fibonacci(n-1)+Fibonacci(n-2); - } - else{ - - return 0; +public static int Fibonacci( int n ) { + if (n < 2) { + return 1; + } + + int first = 0; + int second = 1; + int current = 0; + for (int i = 2; i <= n; i++) { + current = first + second; + first = second; + second = current; + } + return current; } -} ```