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

Commit f7b1494

Browse files
leetcode climbing stairs solution and test
1 parent fed8fa5 commit f7b1494

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package climbing_stairs
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package climbing_stairs
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// https://leetcode.com/problems/n-th-tribonacci-number/
2+
package n_th_tribonacci_number
3+
4+
func tribonacci(n int) int {
5+
if n==0 {
6+
return 0
7+
}
8+
if n<3 {
9+
return 1
10+
}
11+
12+
nums := make([]int, n+1)
13+
nums[0] = 0
14+
nums[1] = 1
15+
nums[2] = 1
16+
17+
for i:=0;i<n-2;i++ {
18+
nums[i+3] = nums[i] + nums[i+1] + nums[i+2]
19+
}
20+
21+
return nums[n]
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package n_th_tribonacci_number
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func Test_tribonacci(t *testing.T) {
8+
cases := []struct {
9+
num int
10+
expected int
11+
}{
12+
{4, 4},
13+
{25, 1389537},
14+
}
15+
16+
for idx, tc := range cases {
17+
res := tribonacci(tc.num)
18+
if tc.expected != res {
19+
t.Errorf("case %d. expecting %d, got %d", idx, tc.expected, res)
20+
}
21+
}
22+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /