121.买卖股票的最佳时机
java
class Solution {
public int maxProfit(int[] prices) {
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for(int i =0;i<prices.length;i++){
if(prices[i] < minprice){
minprice = prices[i]; //找到最小值
}else if(prices[i] - minprice > maxprofit){ //记录之后与最小值之间得差值
maxprofit = prices[i] - minprice;
}
}
return maxprofit;
}
}