From 00dc57faaca1a77fd7c371f2975df5939a7edb0c Mon Sep 17 00:00:00 2001 From: MoonLight-Sherry <3397446353@qq.com> Date: Thu, 31 Mar 2022 10:34:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=860509.=E6=96=90?= =?UTF-8?q?=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0=E5=88=97Rust=E4=B8=A4?= =?UTF-8?q?=E7=A7=8D=E8=A7=A3=E6=B3=95=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\351\202\243\345\245\221\346\225\260.md" | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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 c6ce76c0cd..e60b8a9e5e 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); +} +``` -----------------------