Skip to content

Commit

Permalink
Update 0213.打家劫舍II.md
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenzi committed May 15, 2021
1 parent 92992f0 commit 430d467
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions problems/0213.打家劫舍II.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,28 @@ public:
Java:
```Java
class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0)
return 0;
int len = nums.length;
if (len == 1)
return nums[0];
return Math.max(robAction(nums, 0, len - 1), robAction(nums, 1, len));
}
int robAction(int[] nums, int start, int end) {
int x = 0, y = 0, z = 0;
for (int i = start; i < end; i++) {
y = z;
z = Math.max(y, x + nums[i]);
x = y;
}
return z;
}
}
```

Python:

Expand Down

0 comments on commit 430d467

Please sign in to comment.