Skip to content

Commit 767a384

Browse files
xiyouxiyou
authored andcommitted
update 0070
1 parent 9f7824e commit 767a384

File tree

3 files changed

+39
-85
lines changed

3 files changed

+39
-85
lines changed
Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
1-
package problem0070
1+
use std::collections::HashMap;
2+
pub struct Solution {}
23

3-
func climbStairs(n int) int {
4-
if n < 2 {
5-
return 1
6-
}
4+
fn loop_stairs(n: i32, cacheways: &mut HashMap<i32, i32>) -> i32 {
5+
match cacheways.get(&n) {
6+
Some(v) => return *v,
7+
None => {}
8+
}
79

8-
rec := make([]int, n+1)
9-
rec[0], rec[1] = 1, 1
10+
let mut n1: i32 = 0;
11+
match cacheways.get(&n1) {
12+
Some(v) => n1 = *v,
13+
None => {
14+
n1 = loop_stairs(n - 1, cacheways);
15+
cacheways.insert(n - 1, n1);
16+
}
17+
}
1018

11-
for i := 2; i <= n; i++ {
12-
rec[i] = rec[i-1] + rec[i-2]
13-
}
19+
let mut n2: i32 = 0;
20+
match cacheways.get(&n2) {
21+
Some(v) => n2 = *v,
22+
None => {
23+
n2 = loop_stairs(n - 2, cacheways);
24+
cacheways.insert(n - 2, n2);
25+
}
26+
}
1427

15-
return rec[n]
28+
return n1 + n2;
29+
}
30+
31+
impl Solution {
32+
pub fn climb_stairs(n: i32) -> i32 {
33+
let mut cacheways: HashMap<i32, i32> = HashMap::from([(1, 1), (2, 2)]);
34+
35+
return loop_stairs(n, &mut cacheways);
36+
}
1637
}
Lines changed: 5 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,7 @@
1-
package problem0070
1+
mod climbing_stairs;
2+
use climbing_stairs::Solution;
23

3-
import (
4-
"fmt"
5-
"testing"
6-
7-
"github.com/stretchr/testify/assert"
8-
)
9-
10-
type question struct {
11-
para
12-
ans
13-
}
14-
15-
// para 是参数
16-
type para struct {
17-
n int
18-
}
19-
20-
// ans 是答案
21-
type ans struct {
22-
one int
23-
}
24-
25-
func Test_Problem0070(t *testing.T) {
26-
ast := assert.New(t)
27-
28-
qs := []question{
29-
30-
question{
31-
para{
32-
0,
33-
},
34-
ans{
35-
1,
36-
},
37-
},
38-
39-
question{
40-
para{
41-
10,
42-
},
43-
ans{
44-
89,
45-
},
46-
},
47-
48-
question{
49-
para{
50-
44,
51-
},
52-
ans{
53-
1134903170,
54-
},
55-
},
56-
57-
question{
58-
para{
59-
1,
60-
},
61-
ans{
62-
1,
63-
},
64-
},
65-
66-
// 如需多个测试,可以复制上方元素。
67-
}
68-
69-
for _, q := range qs {
70-
a, p := q.ans, q.para
71-
fmt.Printf("~~%v~~\n", p)
72-
73-
ast.Equal(a.one, climbStairs(p.n), "输入:%v", p)
74-
}
4+
fn main() {
5+
let results = Solution::climb_stairs(4);
6+
println!("{:?}", results);
757
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ leetcode in rust lang.
1717
|2|[Add Two Numbers](./Algorithms/0002.add_two_numbers)|29%|Medium||
1818
|3|[Longest Substring Without Repeating Characters](./Algorithms/0003.longest_substring_without_repeating_characters)|25%|Medium||
1919
|4|[Median of Two Sorted Arrays](./Algorithms/0004.median_of_two_sorted_arrays)|24%|Hard||
20-
|13|[Roman to Integer](./Algorithms/0013.roman_to_integer)|24%|Easy||
20+
|13|[Roman to Integer](./Algorithms/0013.roman_to_integer)|24%|Easy||
21+
|70|[Climbing Stairs](./Algorithms/0070.climbing_stairs)|24%|Easy||

0 commit comments

Comments
 (0)