「力扣」第 370 题:区间加法(会员问题)
参考代码:
import java.util.Arrays;
public class Solution {
public int[] getModifiedArray(int length, int[][] updates) {
int[] b = new int[length + 1];
for (int[] update : updates) {
int left = update[0];
int right = update[1];
int delta = update[2];
b[left] += delta;
b[right + 1] -= delta;
}
// 只能写 < 不能写 <=
for (int i = 1; i < length; i++) {
b[i] += b[i - 1];
}
int[] res = new int[length];
System.arraycopy(b, 0, res, 0, length);
return res;
}
public static void main(String[] args) {
int length = 5;
int[][] updates = {{1, 3, 2}, {2, 4, 3}, {0, 2, -2}};
Solution solution = new Solution();
int[] res = solution.getModifiedArray(length, updates);
System.out.println(Arrays.toString(res));
}
}