Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdda712c

Browse files
authored
Merge pull requestTheAlgorithms#709 from HeikoAlexanderWeber/bug/708,fixTheAlgorithms#708
TheAlgorithms#708 (bugfix for fibonacci sequence function)
2 parents2b3e82f +1031cfb commitdda712c

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

‎Dynamic Programming/Fibonacci.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ public static void main(String[] args) throws Exception {
1717
BufferedReaderbr =newBufferedReader(newInputStreamReader(System.in));
1818
intn =Integer.parseInt(br.readLine());
1919

20-
System.out.println(fibMemo(n));// Returns 8 for n = 6
21-
System.out.println(fibBotUp(n));// Returns 8 for n = 6
20+
// Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
21+
System.out.println(fibMemo(n));
22+
System.out.println(fibBotUp(n));
2223
}
2324

2425
/**
@@ -34,13 +35,12 @@ private static int fibMemo(int n) {
3435

3536
intf;
3637

37-
if (n <=2) {
38-
f =1;
38+
if (n <=1) {
39+
f =n;
3940
}else {
4041
f =fibMemo(n -1) +fibMemo(n -2);
4142
map.put(n,f);
4243
}
43-
4444
returnf;
4545
}
4646

@@ -54,10 +54,10 @@ private static int fibBotUp(int n) {
5454

5555
Map<Integer,Integer>fib =newHashMap<>();
5656

57-
for (inti =1;i <n +1;i++) {
57+
for (inti =0;i <=n;i++) {
5858
intf;
59-
if (i <=2) {
60-
f =1;
59+
if (i <=1) {
60+
f =i;
6161
}else {
6262
f =fib.get(i -1) +fib.get(i -2);
6363
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp