House Robber
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 class HouseRobber { public static void main(String[] args) {
int[] money = {12,23,5,2,434,57,4,767,4,2};
int maximum = houseRobber(money);
System.out.println(maximum);
} private static int houseRobber(int[] num) {
if (0 == num.length) {
return 0;
}
else if (1 == num.length) {
return num[0];
}
else {
num[1] = Math.max(num[0], num[1]);
for (int i = 2; i < num.length; i++) {
num[i] = Math.max(num[i-1], num[i-2] + num[i]); //这里没有想到
}
}
return num[num.length-1];
} }
House Robber的更多相关文章
- [LeetCode] House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- [LeetCode] House Robber II 打家劫舍之二
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- [LeetCode] House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- 【leetcode】House Robber
题目简述 You are a professional robber planning to rob houses along a street. Each house has a certain a ...
- LeetCode House Robber III
原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place ...
- Leetcode House Robber II
本题和House Robber差不多,分成两种情况来解决.第一家是不是偷了,如果偷了,那么最后一家肯定不能偷. class Solution(object): def rob(self, nums): ...
- Leetcode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Java for LeetCode 213 House Robber II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- 【leetcode】House Robber & House Robber II(middle)
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LintCode] House Robber II 打家劫舍之二
After robbing those houses on that street, the thief has found himself a new place for his thievery ...
随机推荐
- jackson对多态or多子类序列化的处理配置
[TOC] Jackson Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json.xml转换成Java对象. 多态类型的处理 jackson允许配置多态类型处理, ...
- 求CRC校验和的低位和高位的两种方式
方式1 unsigned ; // 校验和 ]; memcpy(tstCRCChecksum,&shrCRCCheckSum,); // shrCRCCheckSum:216D LOGI(]) ...
- H5移动前端性能优化
在移动端,因手机的配置和3/4G网络的原因,从两个方面解决性能优化问题,1.加载不超过3秒,用loading或者资源不要超过1M.2.渲染速度. 基于以上两个方面,所有影响首屏加载和渲染的代码应在处理 ...
- SpringMyBatis解析3-MapperFactoryBean
在使用mybatis的时候,我们获取dao的方式一般是这样: SqlSession session=sessionFactory.openSession(); PersonDao personDao= ...
- express-19 路由2
组织路由 在主应用程序文件中定义所有路由太笨重了.那样不仅会导致那个文件一直增长,还不利于功能的分离,因为那个文件里已经有很多东西了. 四条组织路由的指导原则 给路由处理器用命名函数: 到目前为止,我 ...
- DOM--3 DOM核心和DOM2 HTML(2)
核心Node对象 由于继承扩展的关系,DOM中大部分对象会有Node对象的属性和方法,其中包括: nodeName DOM2核心中规定的每种nodeType预期的nodeName值 对象 返回值 El ...
- 手持终端PDA应用固定资产管理系统(资产查询 盘点)软件程序系统
一.产品概述 固定资产管理系统,是针对企事业单位内部资产管理中出现的工作量大.过程繁琐.追踪困难等一系列难题开发的一套先进管理软件.软件实现了对资产的多种方式管理,目前包括条形码.二维码.RFID管理 ...
- CSS3-给网页添加图片
给网页添加图片: 1.background-attachment: scroll--------随文本一块滚动 ; background-attachment: fixed-----固定在一个位置上 ...
- javascript关键字和保留字
1 关键字breakcasecatchcontinuedefaultdeletedoelsefinallyforfunctionifininstanceofnewreturnswitchthisthr ...
- http://blog.csdn.net/jyw935478490/article/details/51233931
http://blog.csdn.net/jyw935478490/article/details/51233931