Leetcode - Range Addition

My code:

public class Solution {
    public int[] getModifiedArray(int length, int[][] updates) {
        if (length <= 0) {
            return null;
        }
        int[] ret = new int[length];
        if (updates == null || updates.length == 0 || updates[0].length != 3) {
            return ret;
        }
        
        for (int i = 0; i < updates.length; i++) {
            int start = updates[i][0];
            int end = updates[i][1];
            int step = updates[i][2];
            ret[start] += step;
            if (end + 1 < ret.length) {
                ret[end + 1] -= step;
            }
        }
        
        for (int i = 1; i < ret.length; i++) {
            ret[i] += ret[i - 1];
        }
        
        return ret;
    }
}

reference:
https://discuss.leetcode.com/topic/49691/java-o-k-n-time-complexity-solution

这道题目没能做出来,直接看的答案。
还是太巧妙了。

Anway, Good luck, Richardo! 09/15/2016

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/e4519bc770f1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞