LeetCode——House Robber
来源:互联网 发布:魔方秀软件下载 编辑:程序博客网 时间:2024/05/18 13:10
题目:
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.
解答:
class Solution {public: int rob(vector<int>& nums) { if (nums.size() <= 0) { return 0; } else if (nums.size() == 1) { return nums[0]; } int notHasLast = nums[0]; int hasLast = nums[1]; for (int i = 2; i <= nums.size() - 1; ++i) { int origNotHasLast = notHasLast; notHasLast += nums[i]; int tmp = notHasLast; notHasLast = hasLast > origNotHasLast ? hasLast : origNotHasLast; hasLast = tmp; } return hasLast > notHasLast ? hasLast : notHasLast; }};
0 0
- LeetCode——House Robber
- LeetCode——House Robber
- LeetCode——House Robber
- LeetCode题解——House Robber III
- Leetcode——198. House Robber
- LeetCode198—House Robber
- leetcode——198——House Robber
- leetcode——213——House Robber II
- leetcode——337—— House Robber III
- LeetCode学习篇十四——House Robber
- 【dp】Leetcode House Robber&& House Robber II
- 【leetcode】House Robber && House Robber II
- LeetCode --- House Robber & House Robber II
- LeetCode198——House Robber
- Leetcode198——House Robber
- DP——House Robber
- Algorithms—198.House Robber
- LeetCode OJ House Robber
- LeetCode 145 Binary Tree Postorder Traversal
- lucas定理
- 【c#第六次作业】猜猜看游戏
- SpringMVC学习(四)
- 给QT 程序的EXE文件添加图标
- LeetCode——House Robber
- 【C语言】字符串右循环移位
- TCP头部解析
- 信号量、互斥体和自旋锁
- div+css截取字符串在规定长度,超出部分用。。。代替
- 在写标签的时候要养成好习惯
- 单向链表的C++实现
- 糊糊论道--毛主席为什么是最伟大的人和兼谈如何“用道”
- HTML输入标签,下拉列表标签,文本域标签