Skip to content

Commit

Permalink
Merge pull request youngyangyang04#2114 from fwqaaq/patch-33
Browse files Browse the repository at this point in the history
Update 0198.打家劫舍.md about rust
  • Loading branch information
youngyangyang04 authored Jun 16, 2023
2 parents ff3f402 + be926a3 commit d979180
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions problems/0198.打家劫舍.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,24 @@ function rob(nums: number[]): number {
};
```

Rust:

```rust
impl Solution {
pub fn rob(nums: Vec<i32>) -> 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]
}
}
```


<p align="center">
Expand Down

0 comments on commit d979180

Please sign in to comment.