Skip to content

Commit e4b415b

Browse files
realDuYuanChaogithub-actions
andauthored
Leetcode 2021.3.23 (examplehub#80)
* n-th-tribonacci-number * n-th-tribonacci-number * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 87fedad commit e4b415b

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
/** https://leetcode.com/problems/n-th-tribonacci-number/ */
4+
public class NthTribonacciNumber {
5+
public static int solution1(int n) {
6+
if (n == 0) {
7+
return 0;
8+
} else if (n == 1) {
9+
return 1;
10+
} else if (n == 2) {
11+
return 1;
12+
} else {
13+
return solution1(n - 1) + solution1(n - 2) + solution1(n - 3);
14+
}
15+
}
16+
17+
public static int solution2(int n) {
18+
int[] nums = new int[n + 3];
19+
nums[0] = 0;
20+
nums[1] = 1;
21+
nums[2] = 1;
22+
for (int i = 3; i <= n; ++i) {
23+
nums[i] = nums[i - 1] + nums[i - 2] + nums[i - 3];
24+
}
25+
return nums[n];
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class NthTribonacciNumberTest {
8+
@Test
9+
void testSolution1() {
10+
assertEquals(NthTribonacciNumber.solution1(0), 0);
11+
assertEquals(NthTribonacciNumber.solution1(1), 1);
12+
assertEquals(NthTribonacciNumber.solution1(2), 1);
13+
assertEquals(NthTribonacciNumber.solution1(4), 4);
14+
assertEquals(NthTribonacciNumber.solution1(25), 1389537);
15+
}
16+
17+
@Test
18+
void testSolution2() {
19+
assertEquals(NthTribonacciNumber.solution2(0), 0);
20+
assertEquals(NthTribonacciNumber.solution2(1), 1);
21+
assertEquals(NthTribonacciNumber.solution2(2), 1);
22+
assertEquals(NthTribonacciNumber.solution2(4), 4);
23+
assertEquals(NthTribonacciNumber.solution2(25), 1389537);
24+
assertEquals(NthTribonacciNumber.solution2(35), 615693474);
25+
}
26+
}

0 commit comments

Comments
 (0)