范围加法

public class Solution {
    public int[] getModifiedArray(int length, int[][] updates) {
        // Write your code here
        int[] changes = new int[length + 1];
        int[] res = new int[length];
        
        for(int i = 0; i < updates.length; i++) {
            changes[updates[i][0]] += updates[i][2];
            changes[updates[i][1] + 1] -= updates[i][2]; 
        }
        
        res[0] = changes[0];
        for(int i = 1; i < length; i++) {
            res[i] = res[i - 1] + changes[i]; 
        }
        
        return res;
    }
}
点赞