classNumMatrix:def__init__(self,matrix:List[List[int]]):self.s=[[0]*(len(matrix[0])+1)for_inrange(len(matrix)+1)]fori,rowinenumerate(matrix,1):forj,xinenumerate(row,1):self.s[i][j]=(self.s[i-1][j]+self.s[i][j-1]-self.s[i-1][j-1]+x)defsumRegion(self,row1:int,col1:int,row2:int,col2:int)->int:return(self.s[row2+1][col2+1]-self.s[row2+1][col1]-self.s[row1][col2+1]+self.s[row1][col1])# Your NumMatrix object will be instantiated and called as such:# obj = NumMatrix(matrix)# param_1 = obj.sumRegion(row1,col1,row2,col2)
1 2 3 4 5 6 7 8 9101112131415161718192021222324
classNumMatrix{privateint[][]s;publicNumMatrix(int[][]matrix){intm=matrix.length;intn=matrix[0].length;s=newint[m+1][n+1];for(inti=1;i<=m;++i){for(intj=1;j<=n;++j){s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+matrix[i-1][j-1];}}}publicintsumRegion(introw1,intcol1,introw2,intcol2){returns[row2+1][col2+1]-s[row2+1][col1]-s[row1][col2+1]+s[row1][col1];}}/** * Your NumMatrix object will be instantiated and called as such: * NumMatrix obj = new NumMatrix(matrix); * int param_1 = obj.sumRegion(row1,col1,row2,col2); */
classNumMatrix{public:NumMatrix(vector<vector<int>>&matrix){intm=matrix.size();intn=matrix[0].size();s.resize(m+1,vector<int>(n+1,0));for(inti=1;i<=m;++i){for(intj=1;j<=n;++j){s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+matrix[i-1][j-1];}}}intsumRegion(introw1,intcol1,introw2,intcol2){returns[row2+1][col2+1]-s[row2+1][col1]-s[row1][col2+1]+s[row1][col1];}private:vector<vector<int>>s;};/** * Your NumMatrix object will be instantiated and called as such: * NumMatrix* obj = new NumMatrix(matrix); * int param_1 = obj->sumRegion(row1,col1,row2,col2); */
typeNumMatrixstruct{s[][]int}funcConstructor(matrix[][]int)NumMatrix{m,n:=len(matrix),len(matrix[0])s:=make([][]int,m+1)fori:=0;i<m+1;i++{s[i]=make([]int,n+1)}fori:=1;i<=m;i++{forj:=1;j<=n;j++{s[i][j]=s[i-1][j]+s[i][j-1]+-s[i-1][j-1]+matrix[i-1][j-1]}}returnNumMatrix{s}}func(this*NumMatrix)SumRegion(row1int,col1int,row2int,col2int)int{returnthis.s[row2+1][col2+1]-this.s[row2+1][col1]-this.s[row1][col2+1]+this.s[row1][col1]}/** * Your NumMatrix object will be instantiated and called as such: * obj := Constructor(matrix); * param_1 := obj.SumRegion(row1,col1,row2,col2); */
classNumMatrix{s:number[][];constructor(matrix:number[][]){constm=matrix.length;constn=matrix[0].length;this.s=newArray(m+1).fill(0).map(()=>newArray(n+1).fill(0));for(leti=1;i<=m;i++){for(letj=1;j<=n;j++){this.s[i][j]=this.s[i-1][j]+this.s[i][j-1]-this.s[i-1][j-1]+matrix[i-1][j-1];}}}sumRegion(row1:number,col1:number,row2:number,col2:number):number{return(this.s[row2+1][col2+1]-this.s[row2+1][col1]-this.s[row1][col2+1]+this.s[row1][col1]);}}/** * Your NumMatrix object will be instantiated and called as such: * var obj = new NumMatrix(matrix) * var param_1 = obj.sumRegion(row1,col1,row2,col2) */
1 2 3 4 5 6 7 8 910111213141516171819202122232425
classNumMatrix{privatevarprefixSum:[[Int]]init(_matrix:[[Int]]){letm=matrix.countletn=matrix[0].countprefixSum=Array(repeating:Array(repeating:0,count:n+1),count:m+1)foriin1...m{forjin1...n{prefixSum[i][j]=prefixSum[i-1][j]+prefixSum[i][j-1]-prefixSum[i-1][j-1]+matrix[i-1][j-1]}}}funcsumRegion(_row1:Int,_col1:Int,_row2:Int,_col2:Int)->Int{returnprefixSum[row2+1][col2+1]-prefixSum[row2+1][col1]-prefixSum[row1][col2+1]+prefixSum[row1][col1]}}/** * Your NumMatrix object will be instantiated and called as such: * let obj = NumMatrix(matrix); * let param_1 = obj.sumRegion(row1,col1,row2,col2); */