题解 | #买卖股票的最好时机 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;
    }
}

全部评论

相关推荐

10-19 00:57
门头沟学院 Java
我不是嘉心糖捏:我刚收到面试捏
投递360集团等公司6个岗位
点赞 评论 收藏
分享
魔法恐龙:这真得给个机会,面试的时候问问不吃饭78.5h怎么做到的
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务