Skip to content

#198: add C++ implementation #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions problems/198.house-robber.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ return b;

## 代码

* 语言支持:JS, C++

JavaScript Code:

```js
/*
* @lc app=leetcode id=198 lang=javascript
Expand Down Expand Up @@ -138,3 +142,23 @@ var rob = function(nums) {
return dp[nums.length + 1];
};
```
C++ Code:
> 与JavaScript代码略有差异,但状态迁移方程是一样的。
```C++
class Solution {
public:
int rob(vector<int>& nums) {
if (nums.empty()) return 0;
auto sz = nums.size();
if (sz == 1) return nums[0];
auto prev = nums[0];
auto cur = max(prev, nums[1]);
for (auto i = 2; i < sz; ++i) {
auto tmp = cur;
cur = max(nums[i] + prev, cur);
prev = tmp;
}
return cur;
}
};
```