java 是我想错了吗,这道题没有那么麻烦啊

股票交易的最大收益

http://www.nowcoder.com/questionTerminal/9e5e3c2603064829b0a0bbfca10594e9

数组后一个减去前一个,为正数就加起来

public class Solution {
    public int maxProfit (int[] prices) {
        int sum = 0;
        for (int i = 1; i < prices.length; i++) {
            int profit = prices[i] - prices[i-1];
            if (profit > 0) {
                sum += profit;
            }
        }
        return sum;
    }
}
全部评论

相关推荐

3 收藏 评论
分享
牛客网
牛客企业服务