力扣直通车
问题描述
代码
package com.leetcode.medium;
/**
* @author 花君瑜
* @date 2022/7/31 21:28:56
**/
public class P1476_SubrectangleQueries {
int[][] rectangle;
//构造函数,对二维数组赋值
public P1476_SubrectangleQueries(int[][] rectangle) {
this.rectangle = rectangle;
}
public 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;
}
}
//输出该二维数组
for (int i = 0; i < rectangle.length; i++) {
for (int j = 0; j < rectangle[i].length; j++) {
System.out.print(rectangle[i][j] + " ");
}
}
System.out.println();
}
public int getValue(int row, int col) {
return rectangle[row][col];
}
public static void main(String[] args) {
int[][] rectangle = {{1, 2, 1}, {4, 3, 4}, {3, 2, 1}, {1, 1, 1}};
P1476_SubrectangleQueries obj = new P1476_SubrectangleQueries(rectangle);
System.out.println(obj.getValue(0, 2));
obj.updateSubrectangle(0, 0, 3, 2, 5);
System.out.println(obj.getValue(0, 2));
System.out.println(obj.getValue(3, 1));
obj.updateSubrectangle(3, 0, 3, 2, 10);
System.out.println(obj.getValue(3, 1));
System.out.println(obj.getValue(0, 2));
}
}