码迷,mamicode.com
首页 > 其他好文 > 详细

【easy】198. House Robber

时间:2018-02-19 12:19:01      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:pre   strong   com   str   好的   最大值   amp   简单   font   

题目一:

一个极其简单的动态规划。

class Solution {
public:
    int rob(vector<int>& nums) {
        int best0 = 0;   // 表示没有选择当前houses  
        int best1 = 0;   // 表示选择了当前houses  
        for(int i = 0; i < nums.size(); i++){  
            int temp = best0;  
            best0 = max(best0, best1); // 没有选择当前houses,那么它等于上次选择了或没选择的最大值  
            best1 = temp + nums[i];    // 选择了当前houses,值只能等于上次没选择的+当前houses的money  
        }  
        return max(best0, best1);  
    }
};

 参考一个很好的blog:http://www.cnblogs.com/grandyang/p/4383632.html

题目二:形成环

参考一个很好的blog:https://www.cnblogs.com/grandyang/p/4518674.html

题目三:沿着二叉树偷的…一种看起来很厉害的偷法

参考一个很好的blog:http://www.cnblogs.com/grandyang/p/5275096.html

【easy】198. House Robber

标签:pre   strong   com   str   好的   最大值   amp   简单   font   

原文地址:https://www.cnblogs.com/sherry-yang/p/8453637.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!