Skip to content

Commit 3778712

Browse files
committed
feat: 0070.Climbing Stairs
1 parent f45839e commit 3778712

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
title: 0070.Climbing Stairs
3+
subtitle: "https://leetcode.com/problems/climbing-stairs/"
4+
date: 2023-12-27T16:34:00+08:00
5+
lastmod: 2023-12-27T16:34:00+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "0070.Climbing-Stairs"
10+
license: ""
11+
images: []
12+
13+
tags: [LeetCode, Go, Easy, Climbing Stairs, DP]
14+
categories: [LeetCode]
15+
16+
featuredImage: ""
17+
featuredImagePreview: ""
18+
19+
hiddenFromHomePage: false
20+
hiddenFromSearch: false
21+
twemoji: false
22+
lightgallery: true
23+
ruby: true
24+
fraction: true
25+
fontawesome: true
26+
linkToMarkdown: false
27+
rssFullText: false
28+
29+
toc:
30+
enable: true
31+
auto: true
32+
code:
33+
copy: true
34+
maxShownLines: 200
35+
math:
36+
enable: false
37+
# ...
38+
mapbox:
39+
# ...
40+
share:
41+
enable: true
42+
# ...
43+
comment:
44+
enable: true
45+
# ...
46+
library:
47+
css:
48+
# someCSS = "some.css"
49+
# located in "assets/"
50+
# Or
51+
# someCSS = "https://cdn.example.com/some.css"
52+
js:
53+
# someJS = "some.js"
54+
# located in "assets/"
55+
# Or
56+
# someJS = "https://cdn.example.com/some.js"
57+
seo:
58+
images: []
59+
# ...
60+
---
61+
# [0070.Climbing Stairs](https://leetcode.com/problems/climbing-stairs/)
62+
63+
## 題目
64+
You are climbing a staircase. It takes n steps to reach the top.
65+
66+
Each time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?
67+
68+
69+
70+
Example 1:
71+
72+
Input: n = 2
73+
Output: 2
74+
Explanation: There are two ways to climb to the top.
75+
1. 1 step + 1 step
76+
2. 2 steps
77+
Example 2:
78+
79+
Input: n = 3
80+
Output: 3
81+
Explanation: There are three ways to climb to the top.
82+
1. 1 step + 1 step + 1 step
83+
2. 1 step + 2 steps
84+
3. 2 steps + 1 step
85+
86+
87+
Constraints:
88+
89+
1 <= n <= 45
90+
91+
* Accepted: 2.8M
92+
* Submissions: 5.4M
93+
* Acceptance Rate: 52.3%
94+
95+
## 題目大意
96+
97+
98+
## 解題思路
99+
100+
* 簡單的 DP,經典的爬樓梯問題. 一個樓梯可以由 n-1 和 n-2 的樓梯爬上來。
101+
* 這一題求解的值就是斐波那契數列。
102+
103+
## Big O
104+
時間複雜 : ``
105+
空間複雜 : ``
106+
107+
## 來源
108+
* https://leetcode.com/problems/climbing-stairs/
109+
* https://leetcode.cn/problems/climbing-stairs/
110+
111+
## 解答
112+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0070.Climbing-Stairs/main.go
113+
114+
```go
115+
116+
```
117+
118+
## Benchmark
119+
120+
```sh
121+
122+
```

Leetcode/0070.Climbing-Stairs/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package climbingstairs
2+
3+
// 時間複雜 O(n), 空間複雜 O(n)
4+
func ClimbStairs(n int) int {
5+
dp := make([]int, n+1)
6+
dp[0], dp[1] = 1, 1
7+
8+
for i := 2; i <= n; i++ {
9+
dp[i] = dp[i-1] + dp[i-2]
10+
}
11+
return dp[n]
12+
}
13+
14+
func ClimbStairsRecursive(n int) int {
15+
dp := make([]int, n+1)
16+
// dp[0], dp[1] = 1, 1
17+
18+
var climbClosure func(n int) int
19+
climbClosure = func(n int) int {
20+
if n <= 2 {
21+
return n
22+
}
23+
dp[n] = climbClosure(n-1) + climbClosure(n-2)
24+
return dp[n]
25+
}
26+
return climbClosure(n)
27+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package climbingstairs
2+
3+
import "testing"
4+
5+
var tests = []struct {
6+
arg1 int
7+
want int
8+
}{
9+
{
10+
2,
11+
2,
12+
},
13+
{
14+
3,
15+
3,
16+
},
17+
}
18+
19+
func TestClimbStairs(t *testing.T) {
20+
for _, tt := range tests {
21+
// if got := ReverseList(tt.arg1); !reflect.DeepEqual(got, tt.want) {
22+
if got := ClimbStairs(tt.arg1); got != tt.want {
23+
t.Errorf("got = %v, want = %v", got, tt.want)
24+
}
25+
}
26+
}
27+
28+
func TestClimbStairsRecursive(t *testing.T) {
29+
for _, tt := range tests {
30+
// if got := ReverseList(tt.arg1); !reflect.DeepEqual(got, tt.want) {
31+
if got := ClimbStairsRecursive(tt.arg1); got != tt.want {
32+
t.Errorf("got = %v, want = %v", got, tt.want)
33+
}
34+
}
35+
}
36+
37+
func BenchmarkClimbStairs(b *testing.B) {
38+
b.ResetTimer()
39+
for i := 0; i < b.N; i++ {
40+
ClimbStairs(tests[0].arg1)
41+
}
42+
}
43+
44+
func BenchmarkClimbStairsRecursive(b *testing.B) {
45+
b.ResetTimer()
46+
for i := 0; i < b.N; i++ {
47+
ClimbStairsRecursive(tests[0].arg1)
48+
}
49+
}
50+
51+
/*
52+
go test -benchmem -run=none LeetcodeGolang/Leetcode/0070.Climbing-Stairs -bench=.
53+
goos: darwin
54+
goarch: amd64
55+
pkg: LeetcodeGolang/Leetcode/0070.Climbing-Stairs
56+
cpu: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
57+
BenchmarkClimbStairs-8 40758399 30.75 ns/op 24 B/op 1 allocs/op
58+
BenchmarkClimbStairsRecursive-8 42196260 27.60 ns/op 24 B/op 1 allocs/op
59+
PASS
60+
ok LeetcodeGolang/Leetcode/0070.Climbing-Stairs 2.489s
61+
*/

0 commit comments

Comments
 (0)