Datawhale leetcode day7 NO_122. 买卖股票的最佳时机 II

在这里插入图片描述

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        i=0;
        j=1;
        money=0;
        while j<len(prices):
            t=prices[j]-prices[i];
            if t>0:
                money = money + t;
                i=i+1;
                j=i+1;
            else:
                if (prices[j]-prices[i])<=0:
                    i=i+1;
                    j=i+1;
        return money;

这是自己写的笨办法。。。

class Solution(object):
    def maxProfit(self, prices):
        return sum(max(prices[i + 1] - prices[i], 0) for i in range(len(prices) - 1))

再看大佬的= =,有毒,一行结束,智商碾压。

猜你喜欢

转载自blog.csdn.net/qq_35547281/article/details/89053474