Skip to content

Latest commit

 

History

History

0308.Range Sum Query 2D - Mutable

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

English Version

题目描述

给你一个 2D 矩阵 matrix,请计算出从左上角 (row1, col1) 到右下角 (row2, col2) 组成的矩形中所有元素的和。

Range Sum Query 2D
上述粉色矩形框内的,该矩形由左上角 (row1, col1) = (2, 1) 和右下角 (row2, col2) = (4, 3) 确定。其中,所包括的元素总和 sum = 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
update(3, 2, 2)
sumRegion(2, 1, 4, 3) -> 10

 

注意:

  1. 矩阵 matrix 的值只能通过 update 函数来进行修改
  2. 你可以默认 update 函数和 sumRegion 函数的调用次数是均匀分布的
  3. 你可以默认 row1 ≤ row2,col1 ≤ col2

 

解法

Python3

Java

...