题目

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:

You may assume that the matrix does not change.

There are many calls to sumRegion function.

You may assume that row1 ≤ row2 and col1 ≤ col2.

分析

思想与上一题相同。

注意下标处理方式。

AC代码

class NumMatrix {
public:
NumMatrix(vector<vector<int>> &matrix) {
if (matrix.empty())
return; //求得二维矩阵的行列
int m = matrix.size(), n = matrix[0].size();
sums = vector<vector<int>>(m+1, vector<int>(n+1, 0));
sums[0][0] = matrix[0][0];
for (int j = 1; j <= n; ++j)
{
sums[0][j] = 0;
} for (int i = 1; i <= m; ++i)
{
sums[i][0] = 0;
} //
for (int i = 1; i <= m; ++i)
{
for (int j = 1; j <= n; ++j)
{
sums[i][j] = sums[i][j - 1] + sums[i - 1][j] - sums[i - 1][j - 1] + matrix[i-1][j-1];
}//for
}//for
} int sumRegion(int row1, int col1, int row2, int col2) {
//求得二维矩阵的行列
int m = sums.size(), n = sums[0].size();
if (row1 < 0 || row1 >= m || col1 < 0 || col1 >= n || row2<0 || row2 >= m ||
col2<0 || col2 >= n || row1 >row2 || col1 > col2)
{
return 0;
} return sums[row2+1][col2+1] - sums[row2+1][col1] - sums[row1][col2+1] + sums[row1][col1];
} private:
vector<vector<int>> sums;
};

GitHub测试程序源码

LeetCode(304)Range Sum Query 2D - Immutable的更多相关文章

  1. LeetCode(307) Range Sum Query - Mutable

    题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...

  2. LeetCode(303)Range Sum Query - Immutable

    题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...

  3. 【刷题-LeetCode】304. Range Sum Query 2D - Immutable

    Range Sum Query 2D - Immutable Given a 2D matrix matrix, find the sum of the elements inside the rec ...

  4. [LeetCode] Range Sum Query - Immutable & Range Sum Query 2D - Immutable

    Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indice ...

  5. [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 预先求和 相似题目 参考资料 日期 题目地址:htt ...

  7. [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 lef ...

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

    Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fac ...

  9. [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 lef ...

随机推荐

  1. Netty(6)关闭

      客户端: public static void main(String[] args) throws Exception { final SslContext sslCtx; if (SSL) { ...

  2. this、apply、call的指向

    1.关于this this它总是返回一个对象,是指调用当前函数的对象,由于对象的属性可以赋给另一个对象,所以属性所在的当前对象是可变的,即this的指向是可变的. var person={ name: ...

  3. SpringBoot整合Redis使用Restful风格实现CRUD功能

    前言 本篇文章主要介绍的是SpringBoot整合Redis,使用Restful风格实现的CRUD功能. Redis 介绍 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-valu ...

  4. 获取jqGrid中选择的行的数据

    下面可以获取选择一行的id,如果你选择多行,那下面的id是最后选择的行的id: var id=$(‘#gridTable’).jqGrid(‘getGridParam’,'selrow’); 如果想要 ...

  5. 【翻译转载】【官方教程】Asp.Net MVC4入门指南(1): 入门介绍

    1. Asp.Net MVC4 入门介绍 · 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/ ...

  6. Android 网络提交数据(使用Asynchronous Http Client)

    项目主页及简单使用方法http://loopj.com/android-async-http/ 页面布局就不复制了,把主要的Activity记录下来,供自己以后使用: package com.exam ...

  7. Android中当item数量超过一定大小RecyclerView高度固定

    Android中当item数量超过一定大小时,将RecyclerView高度固定 方法1 直接通过LayoutParams来设定相应高度 ViewGroup.LayoutParams lp = rv. ...

  8. ubuntu下安装ffmpeg扩展

    可通过PPA进行安装 sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next sudo apt-get update sudo apt-get ...

  9. encryptjs 加密 前端数据(vue 使用 RSA加密、java 后端 RSA解密)

    1.index.html引入 <script src="./static/js/jsencrypt.min.js"></script> 或者 npm i j ...

  10. Java 继承与重写

    1.类的继承 1)继承 父类:所有子类所共有的属性和行为 子类:子类所特有的属性和行为 通过extends关键字来实现类的继承 子类(Sub class)可以继承父类(Super class)的成员变 ...