122. Best Time to Buy and Sell Stock II(买卖股票的最佳时机 II)

题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/

思路:

最初我的理解出了问题,我忘了题目背景是美国股票,可以当天卖再当天买,

我以为是N+1的股市制度,尴尬。。。

比较简单的思路就是只要后一个大于前一个就进行买卖。计算总和即可。

这种思路对于连续涨也适用。

AC 1ms Java:

class Solution {
    public int maxProfit(int[] prices) {
        int max=0;
        for(int i=1;i<prices.length;i++){
            if(prices[i]>prices[i-1])
                max=max+prices[i]-prices[i-1];
        }
        return max;
    }
}

猜你喜欢

转载自blog.csdn.net/God_Mood/article/details/89056023