LeetCodeOJ_198_House Robber_e
2015-06-12 11:06
381 查看
答题链接
from robbing each of them isthat adjacent houses have security system connected and it will automaticallycontact the police if two adjacent houses were broken into on the same night.
Given a list ofnon-negative integers representing the amount of money of each house, determinethe maximum amount of money you can rob tonight without
alerting the police.
分析:
动态规划
代码:
结果:
![](http://img.blog.csdn.net/20150612112847486?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQ4MjQ4Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
题目:
You are a professionalrobber planning to rob houses along a street. Each house has a certain amountof money stashed, the only constraint stopping youfrom robbing each of them isthat adjacent houses have security system connected and it will automaticallycontact the police if two adjacent houses were broken into on the same night.
Given a list ofnon-negative integers representing the amount of money of each house, determinethe maximum amount of money you can rob tonight without
alerting the police.
分析:
动态规划
代码:
<span style="font-size:14px;">class Solution { public: int rob(vector<int>& nums) { int unrobSize=nums.size(); vector<int> robMoney; for(int i=0;i<unrobSize;i++) robMoney.push_back(0); if(unrobSize==0) return 0; if(unrobSize>0) robMoney[0]=nums.at(0); if(unrobSize>1) { if(nums.at(1)>nums.at(0)) robMoney[1]=nums.at(1); else robMoney[1]=nums.at(0); } for(int i=2;i<unrobSize;i++) { int robHouse=nums.at(i)+robMoney.at(i-2); int unrobHouse=robMoney.at(i-1); if(robHouse>unrobHouse) robMoney[i]=robHouse; else robMoney[i]=unrobHouse; } return robMoney[unrobSize-1]; } }; </span>
结果:
相关文章推荐
- 【ActionScript】ActionScript3.0对舞台组件的增删改查
- 在Unity3D里使用DLL
- 操作系统概念学习笔记 10 CPU调度
- 移动互联网领域的技术有哪些:
- iOS不规则按钮实现
- RequireJS学习笔记
- 数据结构存储的顺序和链式对比
- Java环境配置
- 十大推送方式整理
- c语言实现BMP图像转换为灰度图
- 程序员跳槽的10个建议
- SAT数学考题知识范围增量分析
- POJ-1006 Biorhythms (中国剩余定理)
- 瀑布流demo(完美注释)
- GenericServlet为什么有两个init方法
- Android初学习 - 以代码方式(非XML)创建一个布局
- 自定义倒计时TextView控件
- Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法
- 翻转单词顺序列
- C、C++写二进制数据到mysql的Blob字段/插入images