class Solution {
public:
int rob(vector<int> &num) {
if (num.empty())
{
return 0;
}
if (num.size()==1)
{
return num[0];
}
int first = num[0];
int second = num[1];
int res = max(first, second);
auto it = num.begin() + 2;
auto itprev = num.begin() + 1;
for (; it != num.end();++it,++itprev)
{
res = max(max(first + *it, second), second - *itprev + *it);
first = second;
second = res;
}
return res;
}
};Leetcode: House Robber
最新推荐文章于 2017-03-31 18:32:19 发布
本文深入探讨了一种高效的C++算法,用于解决复杂数值序列问题。通过迭代更新变量,实现序列中最大值的快速计算,显著提高了性能。

857

被折叠的 条评论
为什么被折叠?



