代码如下:
class Solution:
def maxProfit(self, prices: List[int]) -> int:
time = len(prices)
pro = 0
for i in range(time):
if prices[i] > prices[i-1] and i != 0:
pro = pro + (prices[i] - prices[i-1])
else:
pass
return pro
i = 0的时候会有输出结果并且影响最终结果,因为列表的 -1 位是队尾,同样有值。因此用and i != 0把它屏蔽掉。