From 0f3ffb3e282af527cd2a529d7921a16321c2561e Mon Sep 17 00:00:00 2001 From: BaoTaoqi <464115280@qq.com> Date: Fri, 8 Jul 2022 10:58:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200746.=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?.md=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0746.使用最小花费爬楼梯.md Rust版本 --- problems/0746.使用最小花费爬楼梯.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index c2c73715..0006f7ac 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -288,6 +288,24 @@ function minCostClimbingStairs(cost: number[]): number { }; ``` +### Rust + +```Rust +use std::cmp::min; +impl Solution { + pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { + let len = cost.len(); + let mut dp = vec![0; len]; + dp[0] = cost[0]; + dp[1] = cost[1]; + for i in 2..len { + dp[i] = min(dp[i-1], dp[i-2]) + cost[i]; + } + min(dp[len-1], dp[len-2]) + } +} +``` + ### C ```c