Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 9 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jun 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update 0213.打家劫舍II.md about rust
  • Loading branch information
fwqaaq authored Jun 2, 2023
commit 477f4595749ea6d38b22f3bac14cdc35c8aa2191
29 changes: 29 additions & 0 deletions problems/0213.打家劫舍II.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,35 @@ function robRange(nums: number[], start: number, end: number): number {
}
```

Rust:

```rust
impl Solution {
pub fn rob(nums: Vec<i32>) -> i32 {
match nums.len() {
1 => nums[0],
_ => Self::rob_range(&nums, 0, nums.len() - 2).max(Self::rob_range(
&nums,
1,
nums.len() - 1,
)),
}
}

pub fn rob_range(nums: &Vec<i32>, start: usize, end: usize) -> i32 {
if start == end {
return nums[start];
}
let mut dp = vec![0; nums.len()];
dp[start] = nums[start];
dp[start + 1] = nums[start].max(nums[start + 1]);
for i in start + 2..=end {
dp[i] = dp[i - 1].max(dp[i - 2] + nums[i]);
}
dp[end]
}
}
```


<p align="center">
Expand Down

AltStyle によって変換されたページ (->オリジナル) /