LeetCode_121. 买卖股票的最佳时机

界限问题 ++i
public class S_121 {
    public int maxProfit(int[] prices) {
        // 为空或者长度为零
        if (prices == null || prices.length < 1) {
            return 0;
        }
        int bene = 0;
        // 上层循环  ++i 先自增
        for(int i = 0;i < prices.length;++i){
            for(int j = i + 1;j < prices.length;++j){
                if(prices[j] - prices[i] > bene)
                    bene = prices[j] - prices[i];
            }
        }
        return bene;
    }
}

猜你喜欢

转载自blog.csdn.net/king1994wzl/article/details/82798762
今日推荐