LeetCode 122_ 买卖股票的最佳时机 II
1. 题目描述
2. 代码
1 class Solution: 2 def maxProfit(self, prices: 'List[int]') -> int: 3 n = len(prices) 4 sums = 0 5 for i in range(1,n): 6 if prices[i] > prices[i-1]: 7 sums += prices[i] - prices[i-1] 8 return sums
思路: 判断数组中后一个元素是否大于前一个元素, 如果大于则累加到sums中.