# LeetCode:House Robber

173人阅读 评论(0)

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

C++代码：

class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
if(n == 0)
return 0;
else if(n == 1)
return nums[0];
else{
vector<int> maxn(n,0);
maxn[0] = nums[0];
maxn[1] = max(nums[0],nums[1]);
for(int i = 2;i < n;++i)
{
maxn[i] = max(maxn[i - 2] + nums[i],maxn[i - 1]);
}
return maxn[n - 1];
}
}
};

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：130117次
• 积分：4553
• 等级：
• 排名：第6791名
• 原创：345篇
• 转载：33篇
• 译文：0篇
• 评论：13条
评论排行
最新评论