首页 > 代码库 > 买卖股票的最佳时机 - C++

买卖股票的最佳时机 - C++

class Solution {
public:
    /**
     * @param prices: Given an integer array
     * @return: Maximum profit
     */
    int maxProfit(vector<int> &prices) {
        // write your code here
        int i,j,m,n,t;
        if(prices.size()==0)
        {
            return 0;
        }
        t = prices.size();
        n=prices[0];
        m = 0;
        for(i=0;i<prices.size()-1;i++)
        {
            if(prices[i+1]<=n)
            {
                n=prices[i+1];
            }
           if(m<=prices[i+1]-n)
           {
               m=prices[i+1]-n;
           }
        }
   
        return m;
    }
};

买卖股票的最佳时机 - C++