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 7800868

Browse files
author
Shuo
authored
Merge pull request #253 from openset/develop
Add: Climbing Stairs
2 parents 020351b + 9584d0c commit 7800868

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
11
package climbing_stairs
2+
3+
func climbStairs(n int) int {
4+
if n == 1 {
5+
return 1
6+
}
7+
first, second := 1, 2
8+
for i := 3; i <= n; i++ {
9+
first, second = second, first+second
10+
}
11+
return second
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
11
package climbing_stairs
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input int
7+
expected int
8+
}
9+
10+
func TestClimbStairs(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: 1,
14+
expected: 1,
15+
},
16+
{
17+
input: 2,
18+
expected: 2,
19+
},
20+
{
21+
input: 3,
22+
expected: 3,
23+
},
24+
}
25+
for _, tc := range tests {
26+
output := climbStairs(tc.input)
27+
if output != tc.expected {
28+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
29+
}
30+
}
31+
}

0 commit comments

Comments
(0)

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