Skip to content

Commit

Permalink
feat: 198.house-robber add Python3 implementation (azl397985856#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybian19 authored and azl397985856 committed Aug 17, 2019
1 parent a7191a8 commit 218bf20
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion problems/198.house-robber.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ return b;

## 代码

* 语言支持:JS, C++
* 语言支持:JS,C++,Python

JavaScript Code:

Expand Down Expand Up @@ -142,8 +142,11 @@ var rob = function(nums) {
return dp[nums.length + 1];
};
```

C++ Code:

> 与JavaScript代码略有差异,但状态迁移方程是一样的。
```C++
class Solution {
public:
Expand All @@ -162,3 +165,22 @@ public:
}
};
```
Python Code:
```python
class Solution:
def rob(self, nums: List[int]) -> int:
if not nums:
return 0
length = len(nums)
if length == 1:
return nums[0]
else:
prev = nums[0]
cur = max(prev, nums[1])
for i in range(2, length):
cur, prev = max(prev + nums[i], cur), cur
return cur
```

0 comments on commit 218bf20

Please sign in to comment.