买卖股票的最佳时机 II

func maxProfit(prices []int) int {
    var max = 0
    
    for i:=1;i<len(prices);i++{
        if prices[i]>prices[i-1]{//只要后一天比前一天有利可图
            max += prices[i]-prices[i-1]//就在前一天买入,在后一天卖出
        }    
    }

    return max
}
posted @ 2021-04-09 14:11  pangqianjin  阅读(33)  评论(0编辑  收藏  举报