Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Autosaved

...

https://leetcode.com/problems/range-sum-query-2d-immutable/

Code Block
languagego
type NumMatrix struct {
    sums [][]int
}


func Constructor(matrix [][]int) NumMatrix {
    sums := make([][]int, len(matrix)+1)
    sums[0] = make([]int, len(matrix[0])+1)
    for i := range matrix {
        sums[i+1] = make([]int, len(matrix[0])+1)

        for j := range matrix[i] {
            sums[i+1][j+1] = sums[i+1][j] + matrix[i][j]
        }
    }
    // fmt.Println(sums)
    for i := range matrix {
        for j := range matrix[i] {
            sums[i+1][j+1] += sums[i][j+1]
        }
    }

    // for i := range sums {
    //     for j := range sums[i] {
    //         fmt.Printf("%2d ", sums[i][j])
    //     }
    //     fmt.Println()
    // }

    return NumMatrix { sums }
}


func (this *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
    return this.sums[row2+1][col2+1] - this.sums[row1][col2+1] - this.sums[row2+1][col1]  +  this.sums[row1][col1]
}


/**
 * Your NumMatrix object will be instantiated and called as such:
 * obj := Constructor(matrix);
 * param_1 := obj.SumRegion(row1,col1,row2,col2);
 */

双指针数组

LC 88. Merge Sorted Array 合并两个有序数组

...