【leetcode】股票的最大利润

int maxProfit(int* prices, int pricesSize){
    int ans = 0;    
    if(pricesSize>=2)
    {                                            //交易日不满两天直接返回0
        int max = prices[pricesSize-1];        //将max初始化为最后一天的值
        int profit;
        for(int i = pricesSize-2;i>=0;i--)
        {                                        //从最后一天开始向第一天遍历
            profit = max-prices[i];             //计算当前的profit
            if(profit<=0)
                max = prices[i];
            else if(profit>ans)
                ans = profit;
        }
    }    
    return ans;
}

 

posted @ 2020-08-17 10:43  温暖了寂寞  阅读(279)  评论(0编辑  收藏  举报