LeetCode 304. Range Sum Query 2D - Immutable

简介: 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。

v2-951847fe9bd615d6c09fc83b9cf1feca_1440w.jpg

Description



Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

Range Sum Query 2D


The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.


Example:


Given matrix = [
  [3, 0, 1, 4, 2],
  [5, 6, 3, 2, 1],
  [1, 2, 0, 1, 5],
  [4, 1, 0, 1, 7],
  [1, 0, 3, 0, 5]
]
sumRegion(2, 1, 4, 3) -> 8
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12


Note:


You may assume that the matrix does not change.

There are many calls to sumRegion function.

You may assume that row1 ≤ row2 and col1 ≤ col2.


描述



给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。


Range Sum Query 2D


上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8。


示例:


给定 matrix = [
  [3, 0, 1, 4, 2],
  [5, 6, 3, 2, 1],
  [1, 2, 0, 1, 5],
  [4, 1, 0, 1, 7],
  [1, 0, 3, 0, 5]
]
sumRegion(2, 1, 4, 3) -> 8
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12


说明:


你可以假设矩阵不可变。

会多次调用 sumRegion 方法。

你可以假设 row1 ≤ row2 且 col1 ≤ col2。


思路



  • 这道题目使用动态规划.
  • 状态:我们声明一个同给定数组一样大小的二维矩阵self.sum,self.sum的每个位置的值self.sum[i][j]表示给定矩阵从matrix[0][0]到matrix[i][j]构成的矩阵的和.
  • 转移方程:我们用temp记录第matirx第i行的前j个数的和,则self.sum[i][j] = temp+self.sum[i-1][j]
  • 答案,area3 - area1 - area2.如下图


v2-7e01273de62a0d12a87482895b12ab6f_720w.jpg


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-02-11 09:27:14
# @Last Modified by:   何睿
# @Last Modified time: 2019-02-11 10:09:38
class NumMatrix:
    def __init__(self, matrix: 'List[List[int]]'):
        # 如果矩阵为空,则记录和的矩阵也置为空
        if not matrix:
            self.sum = [[]]
        else:
            # 矩阵的行数,矩阵的列数
            self.row, self.col = len(matrix), len(matrix[0])
            # self.sum 值self.sum[i][j]表示matrix[0][0]到matrix[i][j]构成矩阵的所有值的和
            self.sum = [[matrix[0][0]] * self.col for _ in range(self.row)]
            # 初始化第一行
            for i in range(1, self.col):
                self.sum[0][i] = self.sum[0][i - 1] + matrix[0][i]
            # 初始化第一列
            for i in range(1, self.row):
                self.sum[i][0] = self.sum[i - 1][0] + matrix[i][0]
            # 填充矩阵的每一个位置
            for i in range(1, self.row):
                temp = matrix[i][0]
                for j in range(1, self.col):
                    temp += matrix[i][j]
                    self.sum[i][j] = self.sum[i - 1][j] + temp
    def sumRegion(self, row1: 'int', col1: 'int', row2: 'int',col2: 'int') -> 'int':
        # 面积1为给定矩阵右下角和matirx[0][0]构成的矩阵
        area1 = self.sum[row2][col2]
        # 面积2为左侧空出来的矩阵
        area2 = self.sum[row2][col1 - 1] if col1 > 0 else 0
        # 面积3为上侧空出来的矩阵,去掉和面积2重叠的部分
        area3 = 0
        if col1 == 0 and row1 != 0: area3 = self.sum[row1 - 1][col2]
        elif row1 != 0 and col1 != 0:
            area3 = self.sum[row1 - 1][col2] - self.sum[row1 - 1][col1 - 1]
        return area1 - area2 - area3


源代码文件在这里.


目录
相关文章
|
5月前
Leetcode 74. Search a 2D Matrix
这道题很简单,为此专门写篇博客其实算博客凑数了。给你一个每一行每一列都是增序,且每一行第一个数都大于上一行末尾数的矩阵,让你判断某个数在这个矩阵中是否存在。 假设矩阵是m*n,扫一遍的时间复杂度就是O(m*n),题目中给出的这么特殊的矩阵,时间复杂度可以降到O(m+n),具体代码如下,写的比较挫。
62 1
LeetCode 307. Range Sum Query - Mutable
update(i, val) 函数可以通过将下标为 i 的数值更新为 val,从而对数列进行修改。
76 0
LeetCode 307. Range Sum Query - Mutable
|
索引
LeetCode 303. Range Sum Query - Immutable
给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点。
66 0
LeetCode 303. Range Sum Query - Immutable
LeetCode 201. Bitwise AND of Numbers Range
给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。
51 0
LeetCode 201. Bitwise AND of Numbers Range
|
算法
LeetCode 74. Search a 2D Matrix
编写一个有效的算法,搜索m×n矩阵中的值。 此矩阵具有以下属性: 每行中的整数从左到右排序. 每行的第一个整数大于前一行的最后一个整数.
50 0
LeetCode 74. Search a 2D Matrix
|
1月前
|
机器学习/深度学习 算法
力扣刷题日常(一)
力扣刷题日常(一)
20 2
|
1月前
|
存储 索引
《LeetCode》—— LeetCode刷题日记
《LeetCode》—— LeetCode刷题日记
|
1月前
|
搜索推荐
《LeetCode》——LeetCode刷题日记3
《LeetCode》——LeetCode刷题日记3
|
1月前
|
容器
《LeetCode》——LeetCode刷题日记1
《LeetCode》——LeetCode刷题日记1
|
1月前
|
存储
实现单链表的基本操作(力扣、牛客刷题的基础&笔试题常客)
实现单链表的基本操作(力扣、牛客刷题的基础&笔试题常客)
143 38

热门文章

最新文章