Java [Leetcode 198]House Robber
2016-01-13 19:38
555 查看
题目描述:
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.
解题思路:
动态规划,设定两个变量,分别表示选择了当前的房子和没有选择当前的房子
代码如下:
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 Solution { public int rob(int[] nums) { int best0 = 0; //choose current house int best1 = 0; // do not choose current house for(int i = 0; i < nums.length; i++){ int temp = best0; best0 = Math.max(best0, best1); best1 = temp + nums[i]; } return Math.max(best0, best1); } }
相关文章推荐
- java socket编程
- Java-JDK环境变量配置教程
- AjaxAnywhere+struts用法
- Java 数组
- New Relic for Java
- myeclipse使用常见问题
- 程序包javax.servlet.annotation不存在
- JAVA中三种URL连接方法
- Java环境变量设置
- Exception:org.eclipse.epp.internal.logging.aeri.ui.log.StandInStacktraceProvider$StandInException
- JAVA WEB filter
- Java 直连Hive Server运行Hive SQL
- 终于我背弃了疯狂JAVA讲义
- spring 的普通类中如何取得session和request对象
- Java URL处理
- maven搭建jersey+Spring4+JPA+Druid+quartz
- RabbitMQ入门教程 For Java【3】 - Publish/Subscribe
- java简单计算器
- Java [Leetcode 101]Symmetric Tree
- 创建你自己的 Java 注解类