解答:
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
last = 0
now = 0
for i in nums:
last, now = now, max(last + i, now)
#last = now
# now = max(last+i, now)
# last = now
return now
总结:
Python连续赋值需要注意的地方
在python中是可以使用连续赋值的方式来一次为多个变量进行赋值的,比如:
a = 3
a, b = 1, a
如果按照正常的思维逻辑,先进行a = 1,在进行b = a,最后b应该等于1,但是这里b应该等于3,因为在连续赋值语句中等式右边其实都是局部变量,而不是真正的变量值本身,比如,上面例子中右边的a,在python解析的时候,只是把变量a的指向的变量3赋给b,而不是a=1之后a的结果.
转载网址:https://blog.csdn.net/qq_34364995/article/details/80544497