Leetcode-121-买卖股票的最佳时机 发表于 2018-09-13 | 分类于 Python , Leetcode “哨兵”的经典操作方法 1234567891011121314151617181920212223class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ if not len(prices): return 0 m = prices[0] n = 0 for val in prices: if val < m: m = val else: profit = val - m if profit > n: n = profit return n 这一题主要提供的思路是创建两个“哨兵”,m用来判断是否有比当前小的值,有的话就进行替换。n用来储存最大的利润值,最终根据列表中两个相邻差值最大的值作为结果返回。