MCPcopy Create free account
hub / github.com/douchuan/algorithm / fib_cache_result

Function fib_cache_result

src/dp/fib.rs:19–32  ·  view source on GitHub ↗
(n: usize)

Source from the content-addressed store, hash-verified

17/// 缓存中间结果
18#[allow(unused)]
19pub fn fib_cache_result(n: usize) -> usize {
20 match n {
21 0 => 0,
22 1 | 2 => 1,
23 _ => match MEMO.with(|memo| memo.borrow()[n]) {
24 0 => {
25 let v = fib_cache_result(n - 1) + fib_cache_result(n - 2);
26 MEMO.with(|memo| memo.borrow_mut()[n] = v);
27 v
28 }
29 memo => memo,
30 },
31 }
32}
33
34/// 只保存前两个值,最节省内存和最快的方式
35#[allow(unused)]

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected