Leetcode(304)——二维区域和检索 - 矩阵不可变

55、宇宙微波背景辐射望远镜及其光学系统解析 本文详细解析了宇宙微波背景辐射(CMB)望远镜及其光学系统的设计与优化。内容涵盖折射与反射光学系统的特性、旁瓣控制技术、气球与地面观测系统的对比、探测器阵列的发展、光学系统设计流程以及不同观测场景下的光学系统选择。同时,文章展望了CMB望远镜光学系统的未来发展趋势,包括技术创新、多频段观测、国际合作与大型项目等。通过系统性的分析与对比,为CMB研究提供了全面的技术支持与理论基础。 阅读详情

Leetcode(304)——二维区域和检索 - 矩阵不可变

题目

给定一个二维矩阵 matrix,以下类型的多个请求:

  • 计算其子矩形范围内元素的总和,该子矩阵的 左上角 为 (row1, col1) ,右下角 为 (row2, col2) 。

实现 NumMatrix 类:

  • NumMatrix(int[][] matrix) 给定整数矩阵 matrix 进行初始化
  • int sumRegion(int row1, int col1, int row2, int col2) 返回 左上角 (row1, col1) 、右下角 (row2, col2) 所描述的子矩阵的元素 总和 。

示例 1:

在这里插入图片描述
输入:
[“NumMatrix”,“sumRegion”,“sumRegion”,“sumRegion”]
[[[[3,0,1,4,2],[5,6,3,2,1],[1,2,0,1,5],[4,1,0,1,7],[1,0,3,0,5]]],[2,1,4,3],[1,1,2,2],[1,2,2,4]]
输出:
[null, 8, 11, 12]
解释:
NumMatrix numMatrix = new NumMatrix([[3,0,1,4,2],[5,6,3,2,1],[1,2,0,1,5],[4,1,0,1,7],[1,0,3,0,5]]);
numMatrix.sumRegion(2, 1, 4, 3); // return 8 (红色矩形框的元素总和)
numMatrix.sumRegion(1, 1, 2, 2); // return 11 (绿色矩形框的元素总和)
numMatrix.sumRegion(1, 2, 2, 4); // return 12 (蓝色矩形框的元素总和)

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 200
  • −105-10^5−105 <= matrix[i][j] <= 10510^5105
  • 0 <= row1 <= row2 < m
  • 0 <= col1 <= col2 < n
  • 最多调用 10410^4104 次 sumRegion 方法

题解

关键:做这种初始化一次、检索多次的题目的秘诀:在初始化的时候做好预处理

方法一:暴力枚举

思路

​​  依旧是熟悉的暴力枚举,我们将 左上角 (row1, col1) 和右下角 (row2, col2) 所构成的矩形中的元素一一进行遍历,然后相加获得最后的值。

代码实现
class NumMatrix {
    vector<vector<int>>& value;
public:
    NumMatrix(vector<vector<int>>& matrix) : value(matrix){}
    
    int sumRegion(int row1, int col1, int row2, int col2) {
        int ans = 0;
        for(int n = row1; n <= row2; n++){     // 行
            for(int m = col1; m <= col2; m++){ // 列
                ans += value[n][m];
            }
        }
        return ans;
    }
};
复杂度分析

时间复杂度:每次查找的时间复杂度都为 O(N∗M)O(N*M)O(N∗M) ,其中 NNN 是左上角与右下角的横坐标之差的绝对值, MMM 是左上角与右下角的纵坐标之差的绝对值。
空间复杂度:O(T)O(T)O(T),TTT 表示输入数组的元素个数。

方法二:二维数组的前缀和

思路

​​  和 Leetcode-303 一样,我们先进行预处理,即求出前缀和,只是这次的前缀和,即 PreSum [i][j] 是指以 (0,0) 为左上角,以 (i,j) 为右下角的矩形中值的和。
​​  那么如何通过前缀和来获得左上角 (row1, col1) 和 右下角 (row2, col2) 所构成的矩形中元素之和呢?
​​  仔细观察可以发现这个公式:
preSum[row2][col2]−preSum[row2][col1−1]−preSum[row1−1][col2]+preSum[row1−1][col1−1]preSum[row2][col2]−preSum[row2][col1−1]−preSum[row1−1][col2]+preSum[row1−1][col1−1]preSum[row2][col2]−preSum[row2][col1−1]−preSum[row1−1][col2]+preSum[row1−1][col1−1]
在这里插入图片描述

​​  但是为了让第 0 行与第 0 列的元素也能使用上面的递推公式。如果 PreSum 矩阵大小和 martix 大小相等,则需要对第 0 行与第 0 列特殊判断。

算法实现:

  1. 在初始化时求出前缀和;
  2. 在调用 sumRegion 时使用公式返回对应值。
代码实现

优化前:(自己最初写的)

class NumMatrix {
    vector<vector<int>> Presum;
public:
    NumMatrix(vector<vector<int>>& matrix) {
        int i = matrix[0].size(), j = matrix.size();
        Presum.resize(j);
        for(int n = 0; n < j; n++){     // 行
            Presum[n].resize(i);
            if(n == 0){
                Presum[n][0] = matrix[n][0];
            }else Presum[n][0] = matrix[n][0] + Presum[n-1][0];
            for(int m = 1; m < i; m++){ // 列
                if(n == 0){
                    Presum[n][m] = Presum[n][m-1] + matrix[n][m];
                }else Presum[n][m] = matrix[n][m] + Presum[n][m-1] + Presum[n-1][m] - Presum[n-1][m-1];
            }
        }
    }
    
    int sumRegion(int row1, int col1, int row2, int col2) {
        if(row1 != 0 && col1 != 0){
            return Presum[row2][col2] - Presum[row1-1][col2] - Presum[row2][col1-1] + Presum[row1-1][col1-1];
        }else if(row1 == 0 && col1 != 0){
            return Presum[row2][col2] - Presum[row2][col1-1];
        }else if(col1 == 0 && row1 != 0){
            return Presum[row2][col2] - Presum[row1-1][col2];
        }else return Presum[row2][col2]; // row1 和 col1 都为 0
    }
};

优化后:

class NumMatrix {
public:
    vector<vector<int>> Presum;

    NumMatrix(vector<vector<int>>& matrix) {
        int m = matrix.size();
        if (m > 0) {
            int n = matrix[0].size();
            Presum.resize(m + 1, vector<int>(n + 1));
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    Presum[i + 1][j + 1] = Presum[i][j + 1] + Presum[i + 1][j] - Presum[i][j] + matrix[i][j];
                }
            }
        }
    }

    int sumRegion(int row1, int col1, int row2, int col2) {
        return Presum[row2 + 1][col2 + 1] - Presum[row1][col2 + 1] - Presum[row2 + 1][col1] + Presum[row1][col1];
    }
};
复杂度分析

时间复杂度:初始化 O(m∗n)O(m*n)O(m∗n),每次检索 O(1)O(1)O(1),其中 mmm 和 nnn 分别是矩阵 matrix\textit{matrix}matrix 的行数和列数。初始化需要遍历矩阵 matrix\textit{matrix}matrix 计算二维前缀和,时间复杂度是 O(mn)O(mn)O(mn)。每次检索的时间复杂度是 O(1)O(1)O(1)。
空间复杂度:O(T)O(T)O(T),TTT 表示输入数组的元素个数。

FLUENT Meshing Watertight Geometry工作流入门 - 9 生成体网格 总结一下我们学到的东西。讨论体积网格的重要性,并了解生成体积网格的不同方法。还了解体积网格质量的重要性,并学习如何改进它。 阅读详情

相关推荐

从零开始!Jupyter Notebook的安装教程(附带pip和Python的安装教程)

本篇文章创造性的以“授人以渔”的方式教大家如何安装Jupyter Notebook、pip和Python,以及对于Jupyter Notebook代码路径的基本设置。

保证安全,保证寿终正寝的博客 13万+

leetcode-304——二维区域和检索-矩阵不可变

在内的前i行前j列的元素和。以上面的数组为例:创建的新数组多一行和一列,方便计算。所以需要根据已经给定的数组创建出一个新数组,这个数组的。因为区域1的面积被减掉了两次。

weixin_44669966的博客 1018

5分钟搞懂语义通信:如何用深度学习让AI理解你的真实意图?

本文深入解析了语义通信如何利用深度学习技术,实现从传统‘比特保真’到‘语义保真’的范式转移。通过介绍基于神经网络的端到端系统架构、编解码技术及实战案例,阐明AI如何理解并高效传递信息核心意图,为6G时代的人机交互奠定基础。

weixin_29191669的博客 266

Leetcode 304. 二维区域和检索 - 矩阵不可变 C++

Leetcode 304. 二维区域和检索 - 矩阵不可变 题目 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。 上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8。 示例: 给定 matrix = [ [3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1,

weixin_43750513的博客 372

RTKLIB PPP与RTK_POST精密定位实战:从实时到事后处理的完整流程解析

本文详细解析了如何使用RTKLIB进行精密单点定位(PPP)的完整流程,涵盖从实时处理到事后处理(RTK_POST)的关键步骤。文章深入探讨了数据准备、软件配置、核心参数设置(如电离层、对流层、潮汐改正)以及常见问题排查,旨在帮助用户掌握高精度GNSS定位技术,实现从入门到精通的实战应用。

weixin_29047919的博客 282

DQN网络代码调用env.reset()后存储观测信息报错问题ValueError: setting an array element with a sequence.解决处理

**reset()函数返回的是一个array类型以及其中数据的type!模拟游戏进行学习任务。因此需要将observation指定为真正需要的array信息即可,得到的的变量observation类型与Agent存储时的。因此怀疑返回的observation有问题,于是通过。报错信息提示为数据维度不对应,也即最初通过。在强化学习DQN网络代码实现过程中,针对。

Vulcan_Q的博客 4093

【LeetCode】304. 二维区域和检索 - 矩阵不可变

day183,二维前缀和 = 多个一维前缀和 / 二维积分图。

weixin_43894455的博客 1058

LTC3783 LED驱动控制器设计与效率优化详解

开关电源控制器是现代LED照明的核心器件,通过电流模式控制实现高效能量转换。其核心原理是通过采样电阻检测负载电流,配合误差放大器形成闭环调节,在汽车电子、显示背光等场景中确保±3%的电流精度。LTC3783作为典型代表,集成升压/降压-升压多拓扑支持,配合PWM调光技术可实现3000:1的无色偏调光范围。在工程实践中,需重点考量功率电感选型、MOSFET开关损耗与热管理设计,例如在38W功率方案中采用10μH/5A电感和4.5mΩ MOSFET可达成95%效率。该方案特别适用于需要应对9-36V宽输入电压波

weixin_36231030的博客 401

LeetCode-Java:303、304区域检索(前缀和)

LeetCode:303、304区域检索,前缀和相关总结

weixin_63505616的博客 944

C#利用S7netplus实现西门子S7_200_SMART PLC高效点位监控与操作

本文详细介绍了如何使用C#结合S7netplus开源库高效监控与操作西门子S7-200 SMART PLC的点位。内容涵盖从开发环境搭建、建立通信连接,到核心的点位读写、批量数据处理及字节与位数组转换等关键技术,并提供了构建稳定监控程序的架构与避坑指南,帮助开发者快速实现上位机与PLC的稳定数据交互。

rum55的博客 316

GD32F470移植LWIP协议,PHY芯片DP83848I

也就是说前24位是有规定的,其中48位MAC中的前几位也是有着对应的含义的。2、旧版LWIP(如1.4.1)存在DHCP状态机卡死问题,建议升级至2.1.3+。

m0_61973119的博客 1591

LeetCode.304二维区域和检索 - 矩阵不可变

LeetCode.304

weixin_62466612的博客 346

【AI工具】免费获取各类AI大模型APIKEY的使用教程,以硅基流动+Deepseek为例

【AI工具】免费获取各类AI大模型APIKEY的使用教程,以硅基流动+Deepseek为例

funfan的博客 4272

304. 二维区域和检索 - 矩阵不可变 - 力扣(LeetCode)

给定一个二维矩阵matrix,以下类型的多个请求:计算其子矩形范围内元素的总和,该子矩阵的为为。实现NumMatrix给定整数矩阵matrix进行初始化返回所描述的子矩阵的元素。输入:输出:​解释:// return 8 (红色矩形框的元素总和)// return 11 (绿色矩形框的元素总和)// return 12 (蓝色矩形框的元素总和)最多调用104次sumRegion方法这道题是求解二维前缀和,用于快速计算二维矩阵中任意子矩阵的和。核心思想是预先计算一个二维前缀和数组。

q1111111da的博客 2230

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

【中等】力扣算法题解析LeetCode304:二维区域和检索-矩阵不可变

LeetCode 304 题要求实现一个类,支持高效查询二维矩阵中任意子矩阵的元素和。通过二维前缀和预处理,将查询时间复杂度优化至O(1)。核心步骤包括: 初始化:构建前缀和数组dp,其中dp[i][j]表示从(0,0)到(i-1,j-1)的矩形和,递推公式为dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1] + matrix[i-1][j-1]。 查询:利用前缀和数组快速计算目标子矩阵和,公式为dp[row2+1][col2+1] - ........

ylumwd的博客 684

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

LeetCode第304题_二维区域和检索-矩阵不可变

LeetCode 304题解析:二维区域和检索问题 📌 题目要求实现一个矩阵类,快速计算任意子矩阵的和。提供了两种解法: 1️⃣ 暴力法:每次查询遍历子矩阵,时间复杂度O(mn) 2️⃣ 前缀和法:预处理二维前缀和数组,查询时间优化至O(1) 🔍 核心思路:利用容斥原理,通过加减不同区域的前缀和快速计算子矩阵和。 💡 代码实现:包含C#、Python、C++三种语言版本,均采用前缀和优化方案。 ⚡ 性能对比:C++实现最快(24ms),Python内存占用最低(17.2MB),C#实现最简洁。 ✅ 适

qq_40263592的博客 1108

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Leetcode-304. 二维区域和检索 - 矩阵不可变

Leetcode-304. 二维区域和检索 - 矩阵不可变     给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。 示例: 给定 matrix = [ [3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [...

WangXu__的博客 345

Leetcode—304. 二维区域和检索 - 矩阵不可变【中等】

Leetcode—304. 二维区域和检索 - 矩阵不可变【中等】

Atticus的博客 450

Leetcode 304.二维区域和检索 - 矩阵不可变(Range Sum Query 2D - Immutable)

Leetcode 304.二维区域和检索 - 矩阵不可变 1 题目描述(Leetcode题目链接)   给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。 上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8。 给定 matri...

qq_39378221的博客 444

LeetCode 304 二维区域和检索 - 矩阵不可变

LeetCode 304 二维区域和检索 - 矩阵不可变 题目链接 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2) 。 上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8。 示例: 给定 matrix = [ [3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4,

旺崽的博客 1989

力扣(LeetCode)每日一题304.二维区域和检索 - 矩阵不可变

304.二维区域和检索 - 矩阵不可变 问题 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1),右下角为 (row2, col2)。 上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8。 示例: 给定 matrix = [ [3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7],

Ackerman2的博客 275

Java实现 LeetCode 304 二维区域和检索 - 矩阵不可变

304. 二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。 Range Sum Query 2D 上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8。 示例: 给定 matrix = [ [3...

南 墙 1万+

LeetCode第304题 - 二维区域和检索 - 矩阵不可变

LeetCode第304题 - 二维区域和检索 - 矩阵不可变

流水账 272

leetcode304. 二维区域和检索 - 矩阵不可变

给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1,col1) ,右下角为 (row2,col2)。 上图子矩阵左上角(row1, col1) = (2, 1),右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8。 示例: 给定 matrix = [ [3, 0, 1, 4, 2], [5, 6, 3, 2, 1], ...

少说,多做。 5541

LeetCode 304. 二维区域和检索 - 矩阵不可变

题目描述 304. 二维区域和检索 - 矩阵不可变 解法:二维前缀和 二维前缀和矩阵中的每一个格子记录的是「以当前位置为区域的右下角(区域左上角恒定为原数组的左上角)的区域和」,那么二维前缀和矩阵就可以按照如下图所示的方法生成 因此当我们要求 (x1,y1)(x1, y_1)(x1,y1​) 作为左上角,(x2,y2)(x_2, y_2)(x2​,y2​) 作为右下角 的区域和的时候,可以直接利用前缀和数组快速求解: sum[x2][y2]−sum[x1−1][y2]−sum[x2][y1−1]+sum[

Aries_young的博客 1041

【力扣】304. 二维区域和检索 - 矩阵不可变

以下为力扣官方题解 304. 二维区域和检索 - 矩阵不可变题目示例:提示:官方题解前言方法一:一维前缀和代码复杂度分析方法二:二维前缀和代码复杂度分析 题目 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1,col1)(row1, col1)(row1,col1) ,右下角为 (row2,col2)(row2, col2)(row2,col2) 。 上图子矩阵左上角 (row1,col1)=(2,1)(row1, col1) = (2, 1)(row1,col1)=(2,

qq_45256357的博客 552
上一篇: Leetcode(303)——区域和检索 - 数组不可变
下一篇: Leetcode(560)——和为 K 的子数组
486L
博客等级 码龄7年 20粉丝 117原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值