public class Solution {
public int maxProfit(int[] prices) {
// Start typing your Java solution below
// DO NOT write main() function
int len = prices.length;
if(len==0||len==1)return 0;
int min = prices[0];
int diff;
int max = 0;
for(int i = 1; i < len; i++){
if(prices[i]<=min)min=prices[i];
else{
diff = prices[i] - min;
if(diff>max) max =diff;
}
}
return max;
}
}
Best Time to Buy and Sell Stock
最新推荐文章于 2022-07-29 20:36:59 发布