Leetcode 495 提莫攻击 线性扫描

  JAVA:

public final int findPoisonedDuration(int[] timeSeries, int duration) {
        if (timeSeries.length == 0 || duration == 0) {
            return 0;
        }
        int cur = 1;
        int re = duration;
        while (cur < timeSeries.length) {
            int time = timeSeries[cur] - timeSeries[cur - 1] + 1;
            if (time > duration) {
                re += duration;
            } else {
                re += time - 1;
            }
            cur++;
        }
        return re;
    }

  JS:

var findPoisonedDuration = function (timeSeries, duration) {
    if (timeSeries.length == 0 || duration == 0) {
        return 0;
    }
    let cur = 1;
    let re = duration;
    while (cur < timeSeries.length) {
        let time = timeSeries[cur] - timeSeries[cur - 1] + 1;
        if (timeSeries > duration) {
            re += duration;
        } else {
            re += time - 1;
        }
        cur++;
    }
    return re;
};

 

posted @ 2020-08-27 22:04  牛有肉  阅读(191)  评论(0编辑  收藏  举报