class Solution {
public int rob(int[] nums) {
if(nums.length==0)
return 0;
if(nums.length==1)
return nums[0];
return Math.max(rob1(nums, 0, nums.length-1), rob1(nums, 1, nums.length));
}
private int rob1(int[] nums, int i, int j){
int[] dp=new int[j];
for(int k=i;k<j;k++)
{
int num1=k>=2?dp[k-2]:0;
int num2=k>=1?dp[k-1]:0;
dp[k]=Math.max(num1+nums[k], num2);
}
return dp[j-1];
}
}
转载于:https://www.cnblogs.com/asuran/p/7797209.html