Leet122. 買賣股票的最佳時機 II(Best Time to Buy and Sell Stock II)

https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/description/

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

貪心即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章