首页 > 其他 > 详细

leetcode-easy-array-122 best time to buy and sell stocks II

时间:2019-06-06 14:49:04      阅读:72      评论:0      收藏:0      [点我收藏+]

mycode  69.45%

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        res = 0
        for i in range(1,len(prices)):
            temp = prices[i] - prices[i-1]
            if temp <= 0:
                continue
            else:
                res += temp
        return res

 

参考:

下面的更快,因为索引查找的次数少一些!

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        if len(prices) == 0:
            return 0
        else:
            profit = 0 
            start = prices[0]
            for i in range(1,len(prices)):
                if prices[i]>start:
                    profit += prices[i] - start
                    start = prices[i]
                else:
                    start = prices[i]
            return profit

 

leetcode-easy-array-122 best time to buy and sell stocks II

原文:https://www.cnblogs.com/rosyYY/p/10984842.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!