1 Star 0 Fork 0

wd6/LeetCode-1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
house-robber-ii.cpp 770 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 10年前 . Update house-robber-ii.cpp
// Time: O(n)
// Space: O(1)
class Solution {
public:
int rob(vector<int>& nums) {
if (nums.size() == 0) {
return 0;
}
if (nums.size() == 1) {
return nums[0];
}
return max(robRange(nums, 0, nums.size() - 1), // Include the first one of nums without the last one.
robRange(nums, 1, nums.size())); // Include the last one of nums without the first one.
}
int robRange(vector<int>& nums, int start, int end) {
int num_i = nums[start], num_i_1 = 0, num_i_2 = 0;
for (int i = start + 1; i < end; ++i) {
num_i_2 = num_i_1;
num_i_1 = num_i;
num_i = max(nums[i] + num_i_2, num_i_1);
}
return num_i;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wd6/LeetCode-1.git
git@gitee.com:wd6/LeetCode-1.git
wd6
LeetCode-1
LeetCode-1
master

搜索帮助