码迷,mamicode.com
首页 > 其他好文 > 详细

122 Best time to buy and sell stock ii

时间:2015-05-22 08:16:27      阅读:105      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!