File tree Expand file tree Collapse file tree
main/java/com/examplehub/leetcode/easy
test/java/com/examplehub/leetcode/easy Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments