【leetcode字节跳动题库】121. Best Time to Buy and Sell Stock

提交代码

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;
    }
}

运行结果

在这里插入图片描述

发布了403 篇原创文章 · 获赞 35 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/AXIMI/article/details/104533084