题解 | #买卖股票的最好时机 ii#

买卖股票的最好时机 ii

https://www.nowcoder.com/practice/572903b1edbd4a33b2716f7649b4ffd4

import java.util.*;


public class Solution {
    /**
     * 
     * @param prices int整型一维数组 
     * @return int整型
     */
    public int maxProfit (int[] prices) {
        int res = 0;
        for (int i = 0; i < prices.length - 1; i++) {
            int length = 1;
            while ((i + length) < prices.length && prices[i + length] > prices[i + length - 1]) {
                length++;
            }
            length--;
            res += prices[i + length] - prices[i];
            i += length;
        }
        return res;
    }
}

全部评论

相关推荐

云边有个小卖铺儿:校招生违约率低,所以我要高😂
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务