码迷,mamicode.com
首页 > 编程语言 > 详细

213. House Robber II Java Solutions

时间:2016-05-02 19:56:30      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

 

 1 public class Solution {
 2     public int rob(int[] nums) {
 3         if(nums == null || nums.length <1) return 0;
 4         if(nums.length == 1) return nums[0];
 5         if(nums.length == 2) return Math.max(nums[0],nums[1]);//将其分为两种情况:1.包括nums[0] 2.包括nums[nums.length-1]
 6         return Math.max(getRob(nums,0,nums.length-2), getRob(nums,1,nums.length-1));
 7     }
 8     
 9     public int getRob(int[] nums,int s,int e){
10         int len = e - s + 1;
11         int[] res = new int[len];
12         res[0] = nums[s];
13         res[1] = Math.max(nums[s],nums[s+1]);
14         for(int i = 2;i<len;i++){
15             res[i] = Math.max(res[i-2]+nums[s+i], res[i-1]);
16         }
17         return res[len-1];
18     }
19 }

 

213. House Robber II Java Solutions

标签:

原文地址:http://www.cnblogs.com/guoguolan/p/5452887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!