Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use std::collections::HashMap;
const MEMO: HashMap<i32, i32> = HashMap::new();
impl Solution {
pub fn climb_stairs(n: i32) -> i32 {
if (MEMO.contains_key(&n)) {
return *MEMO.get(&n).unwrap()
}
if n == 1 {
return 1
}
if n == 2 {
return 2
}
let cache_value = Self::climb_stairs(n-1) + Self::climb_stairs(n-2);
MEMO.insert(n, cache_value);
return cache_value
}
}