首页 > 其他 > 详细

122 Best time to buy and sell stock ii

时间:2015-05-22 08:13:38      阅读:202      评论:0      收藏:0      [点我收藏+]

从前往后扫一遍, 相邻两个数之差大于零的话就加到结果。

public class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
        
        int res = 0;
        for (int i = 0; i < prices.length - 1; i++) {
            int diff = prices[i+1] - prices[i];
            if (diff > 0) {
                res += diff;
            }
        }
        
        return res;
    }
}

 

122 Best time to buy and sell stock ii

原文:http://www.cnblogs.com/77rousongpai/p/4521365.html

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