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 bed7a4eabd..80902559a6 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" @@ -314,7 +314,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] + } +} +```