[Leetcode] 304. Range Sum Query 2D - Immutable 解题报告

RK3308B开发板WiFi+蓝牙一体模组RTL8821CS驱动移植保姆级教程(含DTS配置与避坑点) 本文提供RK3308B开发板RTL8821CS WiFi+蓝牙模组驱动移植的完整教程,涵盖SDK环境配置、DTS设备树设置、内核编译优化及功能验证全流程。重点解析引脚复用冲突、电源管理等关键配置,并给出常见错误解决方案,帮助开发者快速实现双模通信功能。 阅读详情

题目:

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

Range Sum Query 2D
The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.

Example:

Given matrix = [
  [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]
]

sumRegion(2, 1, 4, 3) -> 8
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12

Note:

  1. You may assume that the matrix does not change.
  2. There are many calls to sumRegion function.
  3. You may assume that row1 ≤ row2 and col1 ≤ col2.

思路:

和上一道题目的思路一样,只不过这次是变成了二维的情况:我们用sums[i+1][j+1]表示从(0,0)到(i, j)这个矩形之内的数组元素之和:

1)计算sums数组:sums[i+1][j+1] = sums[i][j+1] + sums[i+1][j] - sums[i][j] + matrix[i][j];时间复杂度是O(mn),其中m和n分别是matrix的行和列个数。

2)计算矩形和:由(row1, col1)到(row2, col2)构成的矩形的和的计算公式是:sumRegion(row1, col1, row2, col2) = sums[row2+1][col2+1] - sums[row1][col2+1] - sums[row2+1][col1] + sums[row1][col1]。时间复杂度是O(1)。

该算法的空间复杂度是O(mn)。时间复杂度应该是对于本题目而言最优的了。

代码:

class NumMatrix {
public:
    NumMatrix(vector<vector<int>> matrix) {
        if (matrix.size() == 0 || matrix[0].size() == 0) {
            return;
        }
        int row_num = matrix.size(), col_num = matrix[0].size();
        sums = vector<vector<int>>(row_num + 1, vector<int>(col_num + 1, 0));
        for(int i = 0; i < row_num; ++i) {
            for (int j = 0; j < col_num; ++j) {
                sums[i + 1][j + 1] = sums[i][j + 1] + sums[i + 1][j] - sums[i][j] + matrix[i][j];
            }
        }
    }
    
    int sumRegion(int row1, int col1, int row2, int col2) {
        return sums[row2 + 1][col2 + 1] - sums[row1][col2 + 1] - sums[row2 + 1][col1] + sums[row1][col1];
    }
private:
    vector<vector<int>> sums;
};

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
 */
大学生作业:80C51存储器数据传输实验:基本指令和数据传送 大学生作业:80C51存储器数据传输实验,报价 200 文章目录实验一、80C51存储器数据传输实验实验目的:实验内容一:基本指令实验内容二:数据传送 实验一、80C51存储器数据传输实验 实验目的: 熟悉软件的集成开发环境 掌握单片机软件设计流程,掌握存储器结构及各窗口之间的联系 掌握内部数据存储器、外部数据存储器的数据传送特点和应用; 实验内容一:基本指令 用三种方法实现将累加器A内容改为20H ORG 0000H LJMP MAIN ORG 0030H MAIN: ;方法一使用寄 阅读详情

相关推荐

Jetson Orin Nano Super开发板实战:视觉模型部署与性能优化指南

本文详细介绍了Jetson Orin Nano Super开发板在边缘AI视觉模型部署与性能优化的完整实战指南。内容涵盖JetPack 6.1环境配置、Docker容器化部署、YOLO11及Transformer模型的TensorRT优化技巧,并通过实测数据展示了性能提升与功耗管理策略,为开发者提供高效的视觉AI部署解决方案。

7h6j5k4l3的博客 1196

【Leetcode】Range Sum Query 2D - Immutable

题目链接:https://leetcode.com/problems/range-sum-query-2d-immutable/ 题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and l

业精于勤,荒于嬉 2686

腾讯Octop:Windows本地AI助手一键安装实战指南

本地AI助手是面向普通用户在个人设备上运行大语言模型的轻量级解决方案,其核心原理在于封装模型加载、推理调度与Web交互等底层复杂性,通过预编译运行时、自适应硬件加速和图形化配置降低使用门槛。技术价值体现在免环境依赖、离线可用、开箱即用,显著提升非开发者在专利撰写、文案生成、技术文档处理等办公场景中的生产力。本文聚焦Windows平台下的腾讯Octop工具,结合qwen2-7b-instruct、phi-3-mini等主流GGUF量化模型,详解安装适配、API集成与专利辅助工作流,覆盖从零部署到工程落地的完整

weixin_29055137的博客 304

[leetcode-304]Range Sum Query 2D - Immutable(java)

问题描述:这里写链接内容问题分析: 这道题与上题相似,稍微麻烦一点,就是每个点的值为以(0,0)为左上角,以该点为右下角的全部和。 当计算(row1,col1)到(row2,col2)时,就是计算newMatrix[row2][col2] - newMatrix[row2-1][col1] - newMatrix[row1][col2-1] + newMatrix[row1-1][col1-1]

中华胡杨的专栏 2030

手把手教你用Python实现最简单的语义通信(Sem Comm)Demo

本文通过Python和PyTorch,手把手教你构建一个基于深度学习的语义通信(Semantic Communication)Demo。该Demo使用自编码器架构,从MNIST图像中提取低维语义特征进行传输与重建,实现了从“传输比特”到“传递含义”的跨越,并探讨了其效率优势与鲁棒性。

weixin_29080843的博客 245

ABAP BDC 处理踩坑笔记:把错误挡在录制之前,把稳定做进每一行 BDCDATA

本文总结了SAP BDC(批输入)开发中的常见问题和解决方案。重点包括:1)理解BDCDATA本质是模拟人工操作而非直接操作数据库;2)数据格式化技巧,如金额、日期、物料号等字段的标准化处理;3)通过CTU_PARAMS参数控制执行环境,确保屏幕尺寸和更新模式一致;4)不要依赖sy-subrc,应通过解析MESSTAB消息判断业务是否成功;5)录制阶段的常见陷阱及解决方法;6)疑难问题的系统性解决思路;7)建议建立完善的错误处理机制,包括日志记录和重跑功能。文章提供了实用的ABAP代码示例和配置建议,帮助开

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 264

LeetCode - 304. Range Sum Query 2D - Immutable

前面已经做过一道Range Sum Query - Immutable的题目,当时我们采用的方法是新建一个数组存储nums中从0到当前index的元素的和,这样在sumRange方法中我们可以在O(1)的时间中得到结果,在题目要求的sumRange多次调用的情况下非常方便。 这里我们同样想要采用相似的方法,于是我们需要另外新建一个二维数组来存储和而不是单个的元素,2D情况下的sumRange分析

找不到工作了... 1325

基于Requests的百度图片搜索爬取技术详解

本文介绍了一种高效获取百度图片搜索数据的混合请求爬虫技术。该技术结合HTML解析与JSON API调用,通过分析百度图片搜索的网络通信模式,实现了首页HTML解析获取初始参数、后续页面直接调用JSON接口的双重机制。系统采用模块化设计,包含Cookies解析、智能请求分发、数据提取、图片下载等核心模块,支持自定义关键词和爬取页数配置。关键技术包括动态参数管理、正则表达式提取、JSON数据处理等,相比传统爬虫方法具有效率高、资源消耗低的优势,为计算机视觉研究提供了可靠的大规模图片数据采集方案。

那些看似不起波澜的日复一日,会在某天让你看到坚持的意义 419

304. Range Sum Query 2D - Immutable

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). The above rectangle (with the red border)

xdhc的博客 268

YOLOv9实战:刀具磨损实时监控从摄像头到预测性维护(附车间落地代码)

摘要: 本文针对制造业刀具磨损监控痛点,提出基于YOLOv9的实时视觉检测方案。通过工业相机采集刀具图像,结合图像预处理(去油污、增强对比度)和深度学习模型,实现磨损等级分类与寿命预测。系统集成硬件选型(抗油污相机、红色环形光源)、数据增强(旋转、噪声模拟)及预测性维护(PLC联动),实测刀具利用率提升30%,工件报废率降至0.5%以下。详解车间部署、模型训练到系统落地的全流程,附代码与避坑指南,为工业视觉项目提供实战参考。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 541

使用安卓的IMU数据进行 行人航位推算(PDR)推算步长 附python 代码

加速度计数据、陀螺仪数据、重力传感器数据、姿态仰角数据。数据分析与定位轨迹生成主要使用Python离线完成。示意图。

三维点云技术探索 1913

leecode 解题总结:304. Range Sum Query 2D - Immutable

#include #include #include #include using namespace std; /* 问题: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower

qingyuanluofeng的专栏 379

十一、增强现实

那都是n年前的老版本了,新版本完全没有这个问题,我就是纠结这个问题,搭建若干环境,又是vmware虚拟机,又是msys2,又是anaconda,又是找机器,折腾了几天,结果通通不是。以一张实际卡片为锚点,通过笔记本的摄像头,在显示器上将一个计算机内的3d物体显示在卡片上,类似于电影上看到的那种全息投影技术;如果你是想在windows环境下开发linux运行程序,那么CygWin/gcc是你的不二之选。而如果你想开发的是windows运行程序,并且追求速度,那么二者相比而言,MinGW是更好的选择。

3188

[leetcode] 304. Range Sum Query 2D - Immutable 解题报告

题目链接:https://leetcode.com/problems/range-sum-query-2d-immutable/ Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lowe

小榕流光的专栏 794

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

【LeetCode】304. Range Sum Query 2D - Immutable 解题报告(Python)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法预先求和相似题目参考资料日期 题目地址:https://leetcode.com/problems/range-sum-query-2d-immutable/description/ 题目描述 Given a 2D matrix matrix, find t...

负雪明烛 2549

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

LeetCode //C - 304. Range Sum Query 2D - Immutable

【代码】LeetCode //C - 304. Range Sum Query 2D - Immutable。

Made in Code 1356

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

LeetCode-304. Range Sum Query 2D - Immutable & 307. Range Sum Query - Mutable

303. Range Sum Query - Immutable class NumArray { int[] accum; public NumArray(int[] nums) { accum=new int[nums.length+1]; for(int i=1;i<=nums.length;i++)...

To_be_thinking的博客 201

【Leetcode】304. Range Sum Query 2D - Immutable

要求设计一个数据结构,可以实现查询左上从。查询的时候,利用前缀和做差即可。范围内矩阵数字的和的操作。列的所有数字的和,规定。

数学、算法爱好者的博客 224

[leetcode-304]Range Sum Query 2D - Immutable

[leetcode-304]Range Sum Query 2D - Immutable

100120101的博客 1698

[LeetCode] 304. Range Sum Query 2D - Immutable

原题链接: https://leetcode.com/problems/range-sum-query-2d-immutable/ 1. 题目介绍 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and ...

Ber03的博客 203

LeetCode 304 Range Sum Query 2D - Immutable(维护二维数组的前缀和)

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). The above rectangle (with the red bo

nudt_oys的博客 1006

LeetCode(304)Range Sum Query 2D - Immutable

题目Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). Range Sum Query 2D The above rectang

指尖飞舞 2802

LeetCode304. Range Sum Query 2D - Immutable

【代码】LeetCode304. Range Sum Query 2D - Immutable。

叶卡捷琳堡的博客 1108

LeetCode: 304. Range Sum Query 2D - Immutable

LeetCode: 304. Range Sum Query 2D - Immutable 题目描述 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row...

杨领well的专栏 184

LeetCode 304. Range Sum Query 2D - Immutable

分析 难度 中 来源 https://leetcode.com/problems/range-sum-query-2d-immutable/ 题目 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and ...

ov流岚vo的专栏 313

LeetCode 304. Range Sum Query 2D - Immutable (Java版; Medium)

welcome to my blog LeetCode Top 100 Liked Questions 304. Range Sum Query 2D - Immutable (Java版; Medium) 题目描述 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its...

littlehaes的博客 254
上一篇: [Leetcode] 303. Range Sum Query - Immutable 解题报告
下一篇: [Leetcode] 305. Number of Islands II 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值