zoukankan      html  css  js  c++  java
  • LeetCode子矩形查询

    LeetCode 子矩形查询

    题目描述

    请你实现一个类SubrectangleQueries,它的构造函数的参数是一个rows * cols的矩形(这里用整数矩阵表示),并支持以下两种操作:

    1. updateSubrectangle(int row1, int col1, int row2, int col2, int newValue)
      • 用 newValue 更新以(row1,col1)为左上角且以(row2,col2)为右下角的子矩形。
    2. getValue(int row, int col)
      • 返回矩形中坐标(row,col)的当前值。

    一得之见(Java)

    /**
     * @author zhkai
     * @date 2021年4月7日09:37:05
     */
    public class SubrectangleQueries {
        private int[][] rect = null;
    
        public SubrectangleQueries(int[][] rectangle) {
            this.rect = rectangle;
        }
    
        /**
         * 用 newValue 更新以(row1,col1)为左上角且以(row2,col2)为右下角的子矩形。
         *
         * @param row1     子矩形左上角行坐标
         * @param col1     子矩形左上角列坐标
         * @param row2     子矩形右下角行坐标
         * @param col2     子矩形右下角列坐标
         * @param newValue 子矩形新值
         */
        public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
            if (rect != null) {
                for (int i = row1; i <= row2; i++) {
                    for (int j = col1; j <= col2; j++) {
                        rect[i][j] = newValue;
                    }
                }
            }
        }
    
        /**
         * 返回矩形中坐标(row,col)的当前值。
         *
         * @param row 行坐标
         * @param col 列坐标
         * @return 当前值
         */
        public int getValue(int row, int col) {
            if (rect != null) {
                return rect[row][col];
            }
            return -1;
        }
    }
    
    

    一得之见(Python)

    from typing import List
    
    
    class SubRectangleQueries:
        def __init__(self, rectangle: List[List[int]]):
            self.data = rectangle
    
        def update_sub_rectangle(
                self,
                row1: int,
                col1: int,
                row2: int,
                col2: int,
                new_value: int):
            """
            用 newValue 更新以(row1,col1)为左上角且以(row2,col2)为右下角的子矩形。
            :param self:
            :param row1: 子矩形左上角行坐标
            :param col1:子矩形左上角列坐标
            :param row2:子矩形右下角行坐标
            :param col2:子矩形右下角列坐标
            :param new_value:子矩形新值
            """
            if self.data is not None:
                for i in range(row1, row2 + 1):
                    for j in range(col1, col2 + 1):
                        self.data[i][j] = new_value
    
        def get_value(self, row, col) -> int:
            """
            回矩形中坐标(row,col)的当前值
            :param self:
            :param row: 行坐标
            :param col: 列坐标
            :return: 当前值
            """
            if self.data is not None:
                return self.data[row][col]
            else:
                return -1
    
    
  • 相关阅读:
    关于在unity中动态获取字符串后在InputField上进行判断的BUG
    关于在将暴风SDK倒入unity中运行程序出现报错问题
    关于用暴风SDK在unity中加入VR效果和利用暴风手柄进行操作
    IDEA 接口无法跳转到实现类
    springboot项目中获取pom中的属性
    mybatisplus异常: 栏位索引超过许可范围:2,栏位数:1。
    七日杀windows服务器搭建
    SQL子查询报错syntax error at end of input
    关于在将excel数据导入到pgsql数据库的时候中文变成问号的处理方式
    字符串补位操作
  • 原文地址:https://www.cnblogs.com/GardenofEden/p/14626770.html
Copyright © 2011-2022 走看看