买卖股票的最佳时机||
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了买卖股票的最佳时机||相关的知识,希望对你有一定的参考价值。
代码(C++):
class Solution {
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
public int maxProfit(int[] prices) {
// write your code here
if (0 == prices.length || null == prices) return 0;
int minPrice = prices[0];
int sumProfit = 0;
for (int i = 1; i < prices.length; i++) {
if (minPrice < prices[i]) {
sumProfit += prices[i] - minPrice;
minPrice = prices[i];
} else {
minPrice = prices[i];
}
}
return sumProfit;
}
};
以上是关于买卖股票的最佳时机||的主要内容,如果未能解决你的问题,请参考以下文章