Skip to content

Commit abbf2eb

Browse files
refactor 509
1 parent 96320dd commit abbf2eb

File tree

2 files changed

+85
-34
lines changed

2 files changed

+85
-34
lines changed

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

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,37 @@
66
/**
77
* 509. Fibonacci Number
88
*
9-
* The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
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,
1011
*
1112
* F(0) = 0, F(1) = 1
1213
* F(N) = F(N - 1) + F(N - 2), for N > 1.
1314
* Given N, calculate F(N).
1415
*
15-
*
16-
*
1716
* Example 1:
18-
*
1917
* Input: 2
2018
* Output: 1
2119
* Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
22-
* Example 2:
2320
*
21+
* Example 2:
2422
* Input: 3
2523
* Output: 2
2624
* Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
27-
* Example 3:
2825
*
26+
* Example 3:
2927
* Input: 4
3028
* Output: 3
3129
* Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
3230
*
33-
*
3431
* Note:
35-
*
3632
* 0 ≤ N ≤ 30.
3733
*/
3834
public class _509 {
3935
public static class Solution1 {
36+
/**
37+
* Time: O(n)
38+
* Space: O(n)
39+
*/
4040
public int fib(int N) {
4141
List<Integer> list = new ArrayList<>();
4242
list.add(0);
@@ -47,4 +47,43 @@ public int fib(int N) {
4747
return list.get(N);
4848
}
4949
}
50+
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];
67+
}
68+
}
69+
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;
87+
}
88+
}
5089
}

src/test/java/com/fishercoder/_509Test.java

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,42 @@
77
import static org.junit.Assert.assertEquals;
88

99
public class _509Test {
10-
private static _509.Solution1 solution1;
11-
12-
@BeforeClass
13-
public static void setup() {
14-
solution1 = new _509.Solution1();
15-
}
16-
17-
@Test
18-
public void test1() {
19-
assertEquals(1, solution1.fib(2));
20-
}
21-
22-
@Test
23-
public void test2() {
24-
assertEquals(2, solution1.fib(3));
25-
}
26-
27-
@Test
28-
public void test3() {
29-
assertEquals(3, solution1.fib(4));
30-
}
31-
32-
@Test
33-
public void test4() {
34-
assertEquals(0, solution1.fib(0));
35-
}
10+
private static _509.Solution1 solution1;
11+
private static _509.Solution2 solution2;
12+
private static _509.Solution3 solution3;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _509.Solution1();
17+
solution2 = new _509.Solution2();
18+
solution3 = new _509.Solution3();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
assertEquals(1, solution1.fib(2));
24+
assertEquals(1, solution2.fib(2));
25+
assertEquals(1, solution3.fib(2));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
assertEquals(2, solution1.fib(3));
31+
assertEquals(2, solution2.fib(3));
32+
assertEquals(2, solution3.fib(3));
33+
}
34+
35+
@Test
36+
public void test3() {
37+
assertEquals(3, solution1.fib(4));
38+
assertEquals(3, solution2.fib(4));
39+
assertEquals(3, solution3.fib(4));
40+
}
41+
42+
@Test
43+
public void test4() {
44+
assertEquals(0, solution1.fib(0));
45+
assertEquals(0, solution2.fib(0));
46+
assertEquals(0, solution3.fib(0));
47+
}
3648
}

0 commit comments

Comments
 (0)