From abbcbe1ed0b52b7232957bf9152ad0e92104114d Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Mon, 10 Apr 2023 15:28:58 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200070.=E7=88=AC=E6=A5=BC=E6=A2=AF.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0070.爬楼梯.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0070.爬楼梯.md b/problems/0070.爬楼梯.md index a06ee91e..c46e3b46 100644 --- a/problems/0070.爬楼梯.md +++ b/problems/0070.爬楼梯.md @@ -470,6 +470,23 @@ impl Solution { } ``` +dp 数组 + +```rust +impl Solution { + pub fn climb_stairs(n: i32) -> i32 { + let n = n as usize; + let mut dp = vec![0; n + 1]; + dp[0] = 1; + dp[1] = 1; + for i in 2..=n { + dp[i] = dp[i - 1] + dp[i - 2]; + } + dp[n] + } +} +``` +

From 03f037afebff9684e9d64283b1d8d5ec690e684a Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Mon, 10 Apr 2023 15:36:23 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200070.=E7=88=AC=E6=A5=BC=E6=A2=AF.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0070.爬楼梯.md | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/problems/0070.爬楼梯.md b/problems/0070.爬楼梯.md index c46e3b46..793d3e67 100644 --- a/problems/0070.爬楼梯.md +++ b/problems/0070.爬楼梯.md @@ -454,19 +454,16 @@ public class Solution { ```rust impl Solution { pub fn climb_stairs(n: i32) -> i32 { - if n <= 2 { + if n <= 1 { return n; } - let mut a = 1; - let mut b = 2; - let mut f = 0; - for i in 2..n { + let (mut a, mut b, mut f) = (1, 1, 0); + for _ in 2..=n { f = a + b; a = b; b = f; } - return f; - } + f } ```