Skip to content

Commit 63393f2

Browse files
refactor 509
1 parent 9c91b17 commit 63393f2

File tree

1 file changed

+49
-77
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+49
-77
lines changed

src/main/java/com/fishercoder/solutions/_509.java

Lines changed: 49 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,59 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
/**
7-
* 509. Fibonacci Number
8-
*
9-
* The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence,
10-
* such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
11-
*
12-
* F(0) = 0, F(1) = 1
13-
* F(N) = F(N - 1) + F(N - 2), for N > 1.
14-
* Given N, calculate F(N).
15-
*
16-
* Example 1:
17-
* Input: 2
18-
* Output: 1
19-
* Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
20-
*
21-
* Example 2:
22-
* Input: 3
23-
* Output: 2
24-
* Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
25-
*
26-
* Example 3:
27-
* Input: 4
28-
* Output: 3
29-
* Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
30-
*
31-
* Note:
32-
* 0 ≤ N ≤ 30.
33-
*/
346
public class _509 {
35-
public static class Solution1 {
36-
/**
37-
* Time: O(n)
38-
* Space: O(n)
39-
*/
40-
public int fib(int N) {
41-
List<Integer> list = new ArrayList<>();
42-
list.add(0);
43-
list.add(1);
44-
for (int i = 2; i <= N; i++) {
45-
list.add(list.get(i - 1) + list.get(i - 2));
46-
}
47-
return list.get(N);
7+
public static class Solution1 {
8+
/**
9+
* Time: O(n)
10+
* Space: O(n)
11+
*/
12+
public int fib(int N) {
13+
List<Integer> list = new ArrayList<>();
14+
list.add(0);
15+
list.add(1);
16+
for (int i = 2; i <= N; i++) {
17+
list.add(list.get(i - 1) + list.get(i - 2));
18+
}
19+
return list.get(N);
20+
}
4821
}
49-
}
5022

51-
public static class Solution2 {
52-
/**
53-
* Time: O(n)
54-
* Space: O(n)
55-
*/
56-
public int fib(int N) {
57-
if (N < 2) {
58-
return N;
59-
}
60-
int[] fibNums = new int[N + 1];
61-
fibNums[0] = 0;
62-
fibNums[1] = 1;
63-
for (int i = 2; i <= N; i++) {
64-
fibNums[i] = fibNums[i - 1] + fibNums[i - 2];
65-
}
66-
return fibNums[N];
23+
public static class Solution2 {
24+
/**
25+
* Time: O(n)
26+
* Space: O(n)
27+
*/
28+
public int fib(int N) {
29+
if (N < 2) {
30+
return N;
31+
}
32+
int[] fibNums = new int[N + 1];
33+
fibNums[0] = 0;
34+
fibNums[1] = 1;
35+
for (int i = 2; i <= N; i++) {
36+
fibNums[i] = fibNums[i - 1] + fibNums[i - 2];
37+
}
38+
return fibNums[N];
39+
}
6740
}
68-
}
6941

70-
public static class Solution3 {
71-
/**
72-
* Time: O(n)
73-
* Space: O(1)
74-
*/
75-
public int fib(int N) {
76-
if (N < 2) {
77-
return N;
78-
}
79-
int a = 0;
80-
int b = 1;
81-
while (N-- > 1) {
82-
int sum = a + b;
83-
a = b;
84-
b = sum;
85-
}
86-
return b;
42+
public static class Solution3 {
43+
/**
44+
* Time: O(n)
45+
* Space: O(1)
46+
*/
47+
public int fib(int N) {
48+
if (N < 2) {
49+
return N;
50+
}
51+
int a = 0;
52+
int b = 1;
53+
while (N-- > 1) {
54+
int sum = a + b;
55+
a = b;
56+
b = sum;
57+
}
58+
return b;
59+
}
8760
}
88-
}
8961
}

0 commit comments

Comments
 (0)