买卖股票的最佳时机 II(122)
法一:
class Solution: def maxProfit(self, prices: List[int]) -> int: ret = 0 for i in range(len(prices)): if i+1<len(prices): if prices[i]<prices[i+1]: ret += prices[i+1]-prices[i] else: break return ret
法二:
class Solution:
def maxProfit(self, prices: List[int]) -> int:
ret = 0
index = prices[0]
for i in prices[1:]:
temp = i-index
index = i
if temp>0:
ret+=temp
return ret