思路:
找到后面与前面的差值最大即可。
代码:
int maxProfit(int* prices, int pricesSize)
{int i = 0, j = 0;//i是后一个最大的,j是前面最小的int max = 0, temp = 0;//表示最大值for (i = 1; i < pricesSize; i++){if (prices[j] < prices[i]){temp = prices[i] - prices[j];}else{j = i;}if (temp > max){max = temp;}}return max;
}