1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public class HouseRobber { public int rob(int[] nums) { if (nums == null || nums.length == 0) { return 0; } int even = 0; int odd = 0; for (int i = 0; i < nums.length; i++) { if (i % 2 == 0) { even += nums[i]; even = Math.max(even, odd); } else { odd += nums[i]; odd = Math.max(even, odd); } } return Math.max(even, odd); } }
|