呓语Beta 2.0 首页 镜头后 灶台前 捐赠者名单 有趣的小站

leetcode 121 Best Time To Buy And Sell Stock

文档信息

leetcode 121 Best Time To Buy And Sell Stock

解法一

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        if len(prices) == 0:
            return 0
        
        ans = 0
        m = prices[0]
        for i in range(1, len(prices)):
            if prices[i] < m:
                m = prices[i]
            elif prices[i] - m > ans:
                ans = prices[i] - m
                
        return ans

note

捐赠支持

可以使用支付宝转帐给richard.ma.19850509#gmail.com(请将#替换为@),谢谢!

Fork me on GitHub