121. Best Time to Buy and Sell Stock

Posted 我的名字叫周周

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了121. Best Time to Buy and Sell Stock相关的知识,希望对你有一定的参考价值。

    /*
     * 121. Best Time to Buy and Sell Stock
     * 这道题目虽然简单,但是自己还是做错了。这道题目自己想多了
     * 首先min和max的顺序并无所谓。另外就是可以从0开始循环,不用等到1开始
     */
    public int maxProfit(int[] prices) {
        int min = Integer.MAX_VALUE, max = 0;
        for (int i = 0; i < prices.length; i++) {
            min = Math.min(min, prices[i]);
            max = Math.max(max, prices[i] - min);
        }
        return max;
    }

 

以上是关于121. Best Time to Buy and Sell Stock的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 121. Best Time to Buy and Sell Stock

121. Best Time to Buy and Sell Stock

121. Best Time to Buy and Sell Stock

121. Best Time to Buy and Sell Stockeasy

121. Best Time to Buy and Sell Stock

121. Best Time to Buy and Sell Stock