题解 | #买卖股票的最好时机(一)#TOP80

import java.util.*;


public class Solution {
    /**
     * 
     * @param prices int整型一维数组 
     * @return int整型
     */
    public int maxProfit (int[] prices) {
        // write code here
        if(prices == null ||prices.length == 1){
            return 0 ;
        }
        int buy = prices[0];
        int max = Integer.MIN_VALUE;
       //买小卖大
        for(int i = 1; i< prices.length; i++){
            //以最小的价格购买
            buy = Math.min(prices[i], buy);
            //当前价格 -  购买价格
            max = Math.max(max, prices[i] - buy);
            
        }
        return max;
    }
}
全部评论

相关推荐

逆流河上万仙退:我觉得佬没必要 学历在这里 去了也不会对履历有很大提升 只是有可能让自己更熟练 是我的话会更倾向于找暑期或者中大厂日常
查看13道真题和解析
点赞 评论 收藏
分享
求面试求offer啊啊啊啊:这个在牛客不是老熟人了吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务