Leetcode - Range Addition

作者: Richardo92 | 来源:发表于2016-10-17 11:44 被阅读77次

    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

    相关文章

      网友评论

        本文标题:Leetcode - Range Addition

        本文链接:https://www.haomeiwen.com/subject/avsrettx.html