买卖股票的最佳时机 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
 

 

posted on 2020-07-31 11:27  不要挡着我晒太阳  阅读(169)  评论(0编辑  收藏  举报

导航