image.png

解决思路

image.png

  1. public int rob(int[] nums){
  2. if (nums.length == 0)
  3. return 0;
  4. if (nums.length == 1)
  5. return nums[0];
  6. return Math.max(myRob(Arrays.copyOfRange(nums,0,nums.length-1)),myRob(Arrays.copyOfRange(nums,1,nums.length)));
  7. }
  8. private int myRob(int[] num){
  9. int preMax = 0;
  10. int currMax = 0;
  11. for (int i:num){
  12. int temp = currMax;
  13. currMax = Math.max(preMax + i,currMax);
  14. preMax = temp;
  15. }
  16. return currMax;
  17. }