- 1 public class Solution {
- 2 public int[] getModifiedArray(int length, int[][] updates) {
- 3 int[] temp = new int[length];
- 4
- for (int[] update: updates) {
- 5 int start = update[0],
- end = update[1] + 1,
- inc = update[2];
- 6 7 temp[start] += inc;
- 8
- if (end < length) 9 temp[end] -= inc;
- 10
- }
- 11 12 int[] result = new int[length];
- 13 int sum = 0;
- 14
- for (int i = 0; i < length; i++) {
- 15 result[i] = sum + temp[i];
- 16 sum = result[i];
- 17
- }
- 18 19
- return result;
- 20
- }
- 21
- }
来源: http://www.bubuko.com/infodetail-1947820.html