第二篇 买卖股票的最佳时机 II

/*
1.你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
2.要能有收益的时候就脱出卖,这样就可以获得最大的利润。
*/


class Solution {
public:
    int maxProfit(vector<int>& prices) {
        //出判断一下是否有误
        if(prices.size()<2){
            return 0;
        }
            
        //定义局部变量 low profit
        int low=prices[0];
        int profit=0;
        for(int i=1;i!=prices.size();++i){
             if(prices[i]>low){
                 profit=profit+prices[i]-low;
             }
            low=prices[i];
        }    
            return profit;  //注意输出的是什么
       /* if(prices.size()<2)return 0;
        int low = prices[0];
        int profit = 0;
        for(int i=1;i!=prices.size();++i){
            if(prices[i]>low){
                profit = profit+prices[i]-low;
            }
            low = prices[i];
        }
        return profit;*/
    }

};

第二篇 买卖股票的最佳时机 II第二篇 买卖股票的最佳时机 II