Leetcode/Java学习笔记

121. Best Time to Buy and Sell S

2018-09-13  本文已影响0人  萧瑟空间

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
解法:

class Solution {
    public int maxProfit(int[] prices) {
        if(prices.length < 2){
            return 0;
        }
        int maxprofit = 0;
        int minprice = prices[0];
        int temp;
        for(int ele: prices){
            temp = ele - minprice;
            if(temp > maxprofit){
                maxprofit = temp;
            }
            if(ele < minprice){
                minprice = ele;
            }
        }
        return maxprofit;
    }
}

第一次做觉得理所当然的方法第二次居然不敢确定这样的做法是不是准确:
有两点值得指出的是:

上一篇 下一篇

猜你喜欢

热点阅读