leetcode-213-线性DP-打家劫舍II 发表于 2020-04-21 | 分类于 数据结构与算法 题目 解法12345678910111213141516171819202122// 动态规划class Solution {public: int rob(vector<int>& nums) { int n = nums.size(); if(n == 0) return 0; if(n == 1) return nums[0]; // 首间房选或不选 return max(helper(nums, 0, n-2), helper(nums,1,n-1)); } int helper(vector<int> &nums, int start, int end){ int memo_i_1 = 0, memo_i_2 = 0; int memo_i = 0; for(int i = end; i >= start; --i){ memo_i = max(nums[i] + memo_i_2, memo_i_1); memo_i_2 = memo_i_1; memo_i_1 = memo_i; } return memo_i; }}; 参考打家劫舍