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