# 【Leetcode】House Robber

2940人阅读 评论(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.

public int rob(int[] nums) {
if(nums.length==0)
return 0;
if(nums.length==1)
return nums[0];
if(nums.length==2)
return Math.max(nums[0], nums[1]);
int n[] = new int[nums.length];
n[0] = nums[0];
n[1]=Math.max(nums[0], nums[1]);
for(int i=2;i<nums.length;i++){
n[i] = Math.max(nums[i]+n[i-2],n[i-1]);
}
return n[nums.length-1];
}  

1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：523665次
• 积分：8278
• 等级：
• 排名：第2610名
• 原创：305篇
• 转载：6篇
• 译文：0篇
• 评论：38条
博客专栏
 Leetcode题解java版 文章：289篇 阅读：457469
文章分类
阅读排行
最新评论