class Solution {
public:
int maxProfit(vector<int>& prices) {
int len = prices.size();
int max = 0;
for(int i=0;i<len-1;i++){
for(int j=i+1;j<len;j++){
if(prices[j]-prices[i]>max){
max = prices[j]-prices[i];
}
}
}
return max;
}
};还有一种方法是从尾部开始搜索,大的就标记为售出价。
本文介绍了一种计算股票买卖最大利润的算法实现。通过两层循环遍历价格数组找到最大收益,另外还提到了一种从尾部开始搜索的优化方法。
384

被折叠的 条评论
为什么被折叠?



