class Solution {
public:
int maxProfit(vector<int>& prices) {
int len = prices.size();
if (len == 0){
return 0;
}
int minpos = 0;
int maxn = 0 ;
int j = 0;
for (int i = 0; i < len; i++){
if (prices[minpos] > prices[i]){
minpos = i;
}
if (maxn < prices[i] - prices[minpos]){
maxn = prices[i] - prices[minpos];
}
}
return maxn;
}
};
121. Best Time to Buy and Sell Stock
最新推荐文章于 2022-02-07 21:21:56 发布