官方答案 class Solution { public: int robRange(vector<int>& nums, int start, int end) { int first = nums[start], second = max(nums[start], nums[start + 1]); for (int i = start + 2; i <= end; i++) { int temp = second; second = max(first + nums[i], second); first = temp; } return second; } int rob(vector<int>& nums) { int length = nums.size(); if (length == 1) { return nums[0]; } else if (length == 2) { return max(nums[0], nums[1]); } return max(robRange(nums, 0, length - 2), robRange(nums, 1, length - 1)); } };