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

全部评论

相关推荐

07-04 09:21
已编辑
Java
推拿大师:这是hr发的钓鱼贴吗
投递字节跳动等公司8个岗位
点赞 评论 收藏
分享
05-11 20:45
门头沟学院 Java
有担当的灰太狼又在摸...:零帧起手查看图片
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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