[LeetCode][类实现]5422. 子矩形查询 原创

算法标签 类实现题目

题目来源 LeetCode

题目简介

在这里插入图片描述

在这里插入图片描述

思路

这个题目唯一尴尬的地方就在于怎么给一个嵌套vector赋值,也就是构造函数怎么写

我们只需要在类内开一个二维变量,然后直接硬等就可以了…
剩下的只需要正常的暴力即可

AC代码

class SubrectangleQueries {
public:
vector<vector<int>> thisRectangle;//1
    SubrectangleQueries(vector<vector<int>>& rectangle) {
        thisRectangle=rectangle;//2
    }
    
    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++)
    		    thisRectangle[i][j] = newValue;
    }
    
    int getValue(int row, int col) {
        return thisRectangle[row][col];
    }
};

/**
 * Your SubrectangleQueries object will be instantiated and called as such:
 * SubrectangleQueries* obj = new SubrectangleQueries(rectangle);
 * obj->updateSubrectangle(row1,col1,row2,col2,newValue);
 * int param_2 = obj->getValue(row,col);
 */

在这里插入图片描述

posted @ 2022-06-19 16:06  俺叫西西弗斯  阅读(0)  评论(0)    收藏  举报  来源