/
Week 3 @ 2025 算法周记【前缀和 + 双指针数组】
  • In progress
  • Week 3 @ 2025 算法周记【前缀和 + 双指针数组】

    前缀和

    LC 303. Range Sum Query - Immutable 区域和检索 - 数组不可变

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

    type NumArray struct { sums []int } func Constructor(nums []int) NumArray { sums := make([]int, len(nums)) sums[0] = nums[0] for i := 1; i < len(nums); i++ { sums[i] = sums[i-1] + nums[i] } return NumArray{sums} } func (this *NumArray) SumRange(left int, right int) int { if left == 0 { return this.sums[right] } return this.sums[right] - this.sums[left-1] } /** * Your NumArray object will be instantiated and called as such: * obj := Constructor(nums); * param_1 := obj.SumRange(left,right); */

    LC 304. Range Sum Query 2D - Immutable 二维区域和检索 - 矩阵不可变

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

    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/

    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/

    待复习

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

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

    未完成

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

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

    待复习

    LC 1260. Shift 2D Grid 二维网格迁移

    https://leetcode.com/problems/shift-2d-grid/

    不引入额外空间做法

    待复习

     

    Related content

    Week 2 @ 2025 算法周记【nSum + 双指针数组】
    Week 2 @ 2025 算法周记【nSum + 双指针数组】
    More like this
    Week 42 @ 2024 算法周记【双指针数组】
    Week 42 @ 2024 算法周记【双指针数组】
    More like this
    Week 47 @ 2024 算法周记【回溯 - 排列/组合/子集】
    Week 47 @ 2024 算法周记【回溯 - 排列/组合/子集】
    More like this
    Week 44 @ 2024 算法周记【二分查找】
    Week 44 @ 2024 算法周记【二分查找】
    More like this
    Week 51 @ 2024 算法周记【双指针链表+丑数】
    Week 51 @ 2024 算法周记【双指针链表+丑数】
    More like this
    Week 43 @ 2024 算法周记【滑动窗口】
    Week 43 @ 2024 算法周记【滑动窗口】
    More like this