题目:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
代码:
class Solution {
public:
int maxProfit(vector& prices) {
if(prices.empty())
{
return 0;
}
int len = prices.size();
int max = 0;
for(int i = 0; i < len - 1;i++)
{
for(int j = i + 1; j < len; j++)
{
int tmp = prices[j] - prices[i];
if(tmp > max)
{
max = tmp;
}
}
}
return max;
}
};
结果: