Skip to content

Commit eaff977

Browse files
committed
开始循环前必须更新一下A[1]的取值
1 parent 95dba25 commit eaff977

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

house_robber.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ def houseRobber(self, A):
1212
'''
1313
if not A:
1414
return 0
15-
if len(A) == 1:
15+
elif len(A) == 1:
1616
return A[0]
17-
if len(A) == 2:
17+
elif len(A) == 2:
1818
return max(A[0], A[1])
19-
for i in xrange(2, len(A)):
20-
A[i] = max(A[i] + A[i - 2], A[i - 1])
21-
return A[-1]
19+
else:
20+
A[1] = max(A[0], A[1])
21+
for i in xrange(2, len(A)):
22+
A[i] = max(A[i] + A[i - 2], A[i - 1])
23+
return A[-1]

0 commit comments

Comments
 (0)