Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit909762f

Browse files
add 1137
1 parent777a5cd commit909762f

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
3030
|5087|[Letter Tile Possibilities](https://leetcode.com/problems/letter-tile-possibilities/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5087.java)| O(1)| O(1)||Medium||
3131
|5083|[Occurrences After Bigram](https://leetcode.com/problems/occurrences-after-bigram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5083.java)| O(n)| O(1)||Easy||
32+
|1137|[N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1137.java)| O(n)| O(n)||Easy||
3233
|1122|[Relative Sort Array](https://leetcode.com/problems/relative-sort-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1122.java)| O(n)| O(n)||Easy||
3334
|1071|[Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1071.java)| O(m*n)| O(1)||Easy||
3435
|1065|[Index Pairs of a String](https://leetcode.com/problems/index-pairs-of-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1065.java)| O(nlogn)| O(1)||Medium||
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
packagecom.fishercoder.solutions;
2+
3+
/**
4+
* 1137. N-th Tribonacci Number
5+
*
6+
* The Tribonacci sequence Tn is defined as follows:
7+
*
8+
* T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.
9+
*
10+
* Given n, return the value of Tn.
11+
*
12+
* Example 1:
13+
* Input: n = 4
14+
* Output: 4
15+
*
16+
* Explanation:
17+
* T_3 = 0 + 1 + 1 = 2
18+
* T_4 = 1 + 1 + 2 = 4
19+
*
20+
* Example 2:
21+
* Input: n = 25
22+
* Output: 1389537
23+
*
24+
* Constraints:
25+
* 0 <= n <= 37
26+
* The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2^31 - 1.
27+
* */
28+
publicclass_1137 {
29+
publicstaticclassSolution1 {
30+
publicinttribonacci(intn) {
31+
if (n <=1) {
32+
returnn;
33+
}
34+
int[]numbers =newint[n +1];
35+
numbers[0] =0;
36+
numbers[1] =1;
37+
numbers[2] =1;
38+
for (inti =3;i <=n;i++) {
39+
numbers[i] =numbers[i -1] +numbers[i -2] +numbers[i -3];
40+
}
41+
returnnumbers[n];
42+
}
43+
}
44+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1137;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1137Test {
10+
privatestatic_1137.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1137.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(2,solution1.tribonacci(3));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(4,solution1.tribonacci(4));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(1389537,solution1.tribonacci(25));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals(0,solution1.tribonacci(0));
35+
}
36+
37+
@Test
38+
publicvoidtest5() {
39+
assertEquals(1,solution1.tribonacci(2));
40+
}
41+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp