public static void beforeMatrix(int[][] afterMatrix) {
int m=afterMatrix.length;
int n=afterMatrix[0].length;
for(int i=m-1;i>=0;i--) {
for(int j=n-1;j>=0;j--) {
int left = j==0 ? 0 : afterMatrix[i][j-1];
int up = i==0 ? 0 : afterMatrix[i-1][j];
int corner = i==0 || j==0 ? 0 : afterMatrix[i-1][j-1];
afterMatrix[i][j]-=left+up-corner;
}
}
}
function beforeMatrix(afterMatrix) {
let m = afterMatrix.length;
let n = afterMatrix[0].length;
for(let i = m - 1; i >= 0; i--) {
for(let j = n - 1; j >= 0; j--) {
let left = j ===0 ? 0 : afterMatrix[i][j-1];
let up = i===0 ? 0 : afterMatrix[i-1][j];
let corner = i===0 || j === 0 ? 0 : afterMatrix[i-1][j-1];
afterMatrix[i][j] -= left + up - corner;
}
}
return afterMatrix;
}
console.log(beforeMatrix([[1,2],[3,4]]));