198. House Robber
来源:互联网 发布:淘宝网宝贝分类怎么弄 编辑:程序博客网 时间:2024/05/01 01:19
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.
找出数组中不相邻元素和的最大值。动态规划:前i个数的最大值为max[i] ,则 max[i]=max(max[i-2]+nums[i],max[i-1]).
public class Solution {
public int rob(int[] nums) {int[] max=new int[nums.length];
if(nums.length==0)
return 0;
else if(nums.length==1)
return nums[0];
max[0]=nums[0];
max[1]=nums[1]>nums[0]?nums[1]:nums[0];
for(int i=2;i<nums.length;i++){
max[i]=(max[i-2]+nums[i])>max[i-1]?(max[i-2]+nums[i]):max[i-1];
}
return max[nums.length-1];
}
}
0 0
- 337. House Robber III+198. House Robber
- 198. House Robber&&213. House Robber II
- 198. House Robber && 213. House Robber II
- [LeetCode]198.House Robber
- 【LeetCode】198. House Robber
- 【LeetCode】198.House Robber
- LeetCode 198. House Robber
- 198.House Robber
- [leetcode] 198.House Robber
- [leetcode] 198. House Robber
- LeetCode:198. House Robber
- 198. House Robber LeetCode
- 198. House Robber
- [LeetCode]198. House Robber
- 198. House Robber
- LeetCode || 198. House Robber
- 198. House Robber
- LeetCode-198. House Robber
- nagios详细安装配置
- 为什么main方法是public static void?
- Responsive设计
- 携程移动端架构演进与优化之路
- SpringMVC接收json数组对象
- 198. House Robber
- hello_world
- 高级依赖配置 - FieldRetrievingFactoryBean
- 让svn diff显示出git diff的彩色效果
- MSP430G2553 WDT的看门狗模式例子
- AD域相关理解(资料来自于网络)
- STL学习--deque
- 关于有符号数到无符号数的强制转换导致的一些问题
- 有人知道eclipse里面 run as 和 Debug as 为什么执行的结果都是一样的呢