Skip to content

Commit

Permalink
Update 0198.打家劫舍.md about rust
Browse files Browse the repository at this point in the history
  • Loading branch information
fwqaaq authored May 31, 2023
1 parent 842bafc commit be926a3
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 @@ -250,7 +250,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 be926a3

Please sign in to comment.