参考链接
- 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)的当前值。
解题思路
题目的要求比较简单,暴力解决就可以实现。也有人将更新的那部分矩阵存起来,到取值的时候再判断,如果范围在更新范围内,则取新矩阵,否则取原矩阵。
代码
class SubrectangleQueries {
public:
vector<vector<int>> rectangle;
SubrectangleQueries(vector<vector<int>>& rectangle) : rectangle(rectangle) {
}
void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
for(int i = row1; i <= row2; i ++)
{
for(int j = col1; j <= col2; j++)
{
rectangle[i][j] = newValue;
}
}
}
int getValue(int row, int col) {
return rectangle[row][col];
}
};