力扣2356.二维差分模板——子矩阵元素加1
-
模板题
- 最后将n+2*n+2的矩阵删去周围一圈变成n*n矩阵的操作
-
class Solution {public:vector<vector<int>> rangeAddQueries(int n, vector<vector<int>>& queries) {vector<vector<int>> res(n+2,vector<int>(n+2));for(auto t : queries){int r1=t[0],c1=t[1],r2=t[2]+1,c2=t[3]+1;res[r1+1][c1+1] ++; res[r1+1][c2+1] --;res[r2+1][c1+1] --;res[r2+1][c2+1] ++;}for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)res[i][j] += res[i][j-1] + res[i-1][j] - res[i-1][j-1];//删上下两边res.pop_back() , res.erase(res.begin());//每一行删去头尾for(auto &row:res)row.pop_back(),row.erase(row.begin());return res;}};