Skip to content

Commit 14b7c2d

Browse files
add 509
1 parent b43170b commit 14b7c2d

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ Your ideas/fixes/algorithms are more than welcome!
267267
|515|[Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_515.java) | O(n) |O(k) | |Medium| BFS
268268
|514|[Freedom Trail](https://leetcode.com/problems/freedom-trail/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_514.java) | O(?) |O(?) | |Hard | DP
269269
|513|[Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_513.java) | O(n) |O(k) | |Medium| BFS
270+
|509|[Fibonacci Number](https://leetcode.com/problems/fibonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_509.java) | O(n) |O(n) | |Easy| Array
270271
|508|[Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_508.java) | O(n) |O(n) | |Medium| DFS, Tree
271272
|507|[Perfect Number](https://leetcode.com/problems/perfect-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_507.java) | O(sqrt(n)) |O(1) | |Easy| Math
272273
|506|[Relative Ranks](https://leetcode.com/problems/relative-ranks/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_506.java) | O(nlogn) |O(n) | |Easy|
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 509. Fibonacci Number
8+
*
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,
10+
*
11+
* F(0) = 0, F(1) = 1
12+
* F(N) = F(N - 1) + F(N - 2), for N > 1.
13+
* Given N, calculate F(N).
14+
*
15+
*
16+
*
17+
* Example 1:
18+
*
19+
* Input: 2
20+
* Output: 1
21+
* Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
22+
* Example 2:
23+
*
24+
* Input: 3
25+
* Output: 2
26+
* Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
27+
* Example 3:
28+
*
29+
* Input: 4
30+
* Output: 3
31+
* Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
32+
*
33+
*
34+
* Note:
35+
*
36+
* 0 ≤ N ≤ 30.
37+
*/
38+
public class _509 {
39+
public static class Solution1 {
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);
48+
}
49+
}
50+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._509;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
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+
}
36+
}

0 commit comments

Comments
 (0)