diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" index 638bfdfe20..d339940cf0 100644 --- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" +++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" @@ -277,7 +277,30 @@ int fib(int n){ return fib(n-1) + fib(n-2); } ``` +### Rust +动态规划: +```Rust +pub fn fib(n: i32) -> i32 { + let n = n as usize; + let mut dp = vec![0; 31]; + dp[1] = 1; + for i in 2..=n { + dp[i] = dp[i - 1] + dp[i - 2]; + } + dp[n] +} +``` - +递归实现: +```Rust +pub fn fib(n: i32) -> i32 { + //若n小于等于1,返回n + f n <= 1 { + return n; + } + //否则返回fib(n-1) + fib(n-2) + return fib(n - 1) + fib(n - 2); +} +``` -----------------------