https://leetcode-cn.com/problems/subrectangle-queries/
请你实现一个类 SubrectangleQueries ,它的构造函数的参数是一个 rows x cols 的矩形(这里用整数矩阵表示),并支持以下两种操作:
- updateSubrectangle(int row1, int col1, int row2, int col2, int newValue)
用 newValue 更新以 (row1,col1) 为左上角且以 (row2,col2) 为右下角的子矩形。
2. getValue(int row, int col)
返回矩形中坐标 (row,col) 的当前值。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/subrectangle-queries
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路:更新操作时只存储不进行更新,最后直接顺序查找更新,算是一类题目。
class SubrectangleQueries:
def __init__(self, rectangle: List[List[int]]):
self.rec=rectangle
self.hist=[]
def updateSubrectangle(self, row1: int, col1: int, row2: int, col2: int, newValue: int) -> None:
self.hist.append([row1,col1,row2,col2,newValue])
def getValue(self, row: int, col: int) -> int:
flag=0
answer=0
for i in self.hist:
if row>=i[0] and col>=i[1] and row<=i[2] and col <=i[3]:
answer= i[4]
flag=1
if flag==1:
return answer
return self.rec[row][col]
# Your SubrectangleQueries object will be instantiated and called as such:
# obj = SubrectangleQueries(rectangle)
# obj.updateSubrectangle(row1,col1,row2,col2,newValue)
# param_2 = obj.getValue(row,col)