class Solution { public: /* 参考打家劫舍I,封装函数,分别去头去尾计算完了求最大值即可 */ int maxValue(int a, int b, vector<int>& nums){ int prev = 0, cur = 0, tmp = 0; for(int i = a; i <= b; ++i){ tmp = cur; cur = max(cur, nums[i] + prev); prev = tmp; } return cur; } int rob(vector<int>& nums) { return max(maxValue(1, nums.size()-1, nums), maxValue(0, nums.size() - 2, nums)); } };