From be926a3c5b3e67023cf99f4f0311d091c01a7e08 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Wed, 31 May 2023 21:43:25 +0800 Subject: [PATCH] =?UTF-8?q?Update=200198.=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8D.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\223\345\256\266\345\212\253\350\210\215.md" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index 6e682ec39e..1ecd2eb716 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -250,7 +250,24 @@ function rob(nums: number[]): number { }; ``` +Rust: +```rust +impl Solution { + pub fn rob(nums: Vec) -> i32 { + if nums.len() == 1 { + return nums[0]; + } + let mut dp = vec![0; nums.len()]; + dp[0] = nums[0]; + dp[1] = nums[0].max(nums[1]); + for i in 2..nums.len() { + dp[i] = (dp[i - 2] + nums[i]).max(dp[i - 1]); + } + dp[nums.len() - 1] + } +} +```