题目
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210527125924648.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0FYSU1J,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210527125934577.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0FYSU1J,size_16,color_FFFFFF,t_70)
题目链接
提交代码
class Solution {
public int maxProfit(int[] prices) {
if(prices==null||prices.length==0) return 0;
int res=0,in=prices[0],out=prices[0];
for(Integer price:prices) {
if(price<in) {
in=price;
out=0;
}
else if(price>out) out=price;
res=out-in>res?out-in:res;
}
return res;
}
}
运行结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200227114226568.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0FYSU1J,size_16,color_FFFFFF,t_70)