其他分享
首页 > 其他分享> > LeetCode 198. 打家劫舍 House Robber

LeetCode 198. 打家劫舍 House Robber

作者:互联网

 

 并不是简单的奇偶位之和。

class Solution {
public:
    int rob(vector<int>& nums) {
        if (nums.empty()) return 0;
        int size = nums.size();
        if (size == 1) return nums[0];
        int first = nums[0], second = max(nums[0], nums[1]);
        for (int i = 2; i < size; ++i)
        {
            int temp = second;
            second = max(first + nums[i], second);
            first = temp;
        }
        return second;
    }
};

 

标签:return,nums,int,House,second,first,LeetCode,Robber,size
来源: https://www.cnblogs.com/ZSY-blog/p/12989547.html