Skip to content

Commit 218bf20

Browse files
ybian19azl397985856
authored andcommitted
feat: 198.house-robber add Python3 implementation (azl397985856#120)
1 parent a7191a8 commit 218bf20

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

problems/198.house-robber.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ return b;
7777

7878
## 代码
7979

80-
* 语言支持:JS, C++
80+
* 语言支持:JS,C++,Python
8181

8282
JavaScript Code:
8383

@@ -142,8 +142,11 @@ var rob = function(nums) {
142142
return dp[nums.length + 1];
143143
};
144144
```
145+
145146
C++ Code:
147+
146148
> 与JavaScript代码略有差异,但状态迁移方程是一样的。
149+
147150
```C++
148151
class Solution {
149152
public:
@@ -162,3 +165,22 @@ public:
162165
}
163166
};
164167
```
168+
169+
Python Code:
170+
171+
```python
172+
class Solution:
173+
def rob(self, nums: List[int]) -> int:
174+
if not nums:
175+
return 0
176+
177+
length = len(nums)
178+
if length == 1:
179+
return nums[0]
180+
else:
181+
prev = nums[0]
182+
cur = max(prev, nums[1])
183+
for i in range(2, length):
184+
cur, prev = max(prev + nums[i], cur), cur
185+
return cur
186+
```

0 commit comments

Comments
 (0)