Update 0213.打家劫舍II.md

This commit is contained in:
zhenzi
2021-05-15 11:47:15 +08:00
parent 92992f0590
commit 430d467494

View File

@ -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