首页 > 其他 > 详细

121. Best Time to Buy and Sell Stock

时间:2016-05-22 00:29:35      阅读:207      评论:0      收藏:0      [点我收藏+]
    /*
     * 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

原文:http://www.cnblogs.com/zmyvszk/p/5515878.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!