Versions Compared

Key

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

...

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 合并两个有序数组

https://leetcode.com/problems/merge-sorted-array/ & More

Code Block
languagego
func merge(nums1 []int, m int, nums2 []int, n int)  {
    i, j := m-1, n-1
    p := m+n-1
    for i >= 0 && j >= 0 {
        if nums1[i] >= nums2[j] {
            nums1[p] = nums1[i]
            p--
            i--
        } else {
            nums1[p] = nums2[j]
            p--
            j--
        }
    }
    for i >= 0 {
        nums1[p] = nums1[i]
        p--
        i--
    }
    for j >= 0 {
        nums1[p] = nums2[j]
        p--
        j--
    }
}

LC 977. Squares of a Sorted Array 有序数组的平方

https://leetcode.com/problems/squares-of-a-sorted-array/description/

Status
colourBlue
title待复习

Code Block
languagego
func sortedSquares(nums []int) []int {
    n := len(nums)
    i, j := 0, n-1
    result := make([]int, n)
    p := n-1

    for i <= j {
        a := nums[i] * nums[i]
        b := nums[j] * nums[j]
        if a >= b {
            result[p] = a
            p--
            i++
        } else {
            result[p] = b
            p--
            j--
        }
    }
    
    return result
}

LC 360. 有序转化数组 [Premium]

https://leetcode.com/problems/sort-transformed-array/description/

Status
title未完成

LC 1329. 将矩阵按对角线排序

https://leetcode.com/problems/sort-the-matrix-diagonally/description/

– 剩下 2 题