this.matrix
로 생성 인자를 할당한다.row1
<= row2
, col1
<= col2
가 보장되어 있으므로 사잇값을 순회하며 모든 인자를 더한다.class NumMatrix {
private matrix: number[][]
constructor(matrix: number[][]) {
this.matrix = matrix
}
sumRegion(row1: number, col1: number, row2: number, col2: number): number {
let sum = 0
for(let row = row1; row <= row2; row++) {
for(let col = col1; col <= col2; col++) {
sum += this.matrix[row][col]
}
}
return sum
}
}