int maxProfit(int* prices, int pricesLen ) { //思路 //记录买点和最大利润,假设当前价格是卖点,依次后移计算,超过当前利润则进行更新。 int buyIn; int max; if ((pricesLen <= 1) || (prices == NULL)) { return 0; } buyIn = prices[0]; max = 0; for (int i = 0; i < pricesLen; i++) { if (prices[i] - buyIn > max) { max = prices[i] - buyIn; //更新最大利润 } if (prices[i] < buyIn) { buyIn = prices[i]; //更新买点 } } return max; }