[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 indices i and j (i ≤ j), inclusive.

Example:

Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3

Note:

  1. You may assume that the array does not change.
  2. There are many calls to sumRange function.
 1 class NumArray {
 2 private:
 3     vector<int> acc;
 4
 5 public:
 6     NumArray(vector<int> &nums) {
 7         acc.push_back(0);
 8         for (auto n : nums) {
 9             acc.push_back(acc.back() + n);
10         }
11     }
12
13     int sumRange(int i, int j) {
14         return acc[j + 1] - acc[i];
15     }
16 };
17
18
19 // Your NumArray object will be instantiated and called as such:
20 // NumArray numArray(nums);
21 // numArray.sumRange(0, 1);
22 // numArray.sumRange(1, 2);

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) 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.
 1 class NumMatrix {
 2 private:
 3     vector<vector<int>> acc;
 4 public:
 5     NumMatrix(vector<vector<int>> &matrix) {
 6         if (matrix.empty()) return;
 7         int n = matrix.size(), m = matrix[0].size();
 8         acc.resize(n + 1, vector<int>(m + 1));
 9         for (int i = 0; i <= n; ++i) acc[i][0] = 0;
10         for (int j = 0; j <= m; ++j) acc[0][j] = 0;
11         for (int i = 1; i <= n; ++i) {
12             for (int j = 1; j <= m; ++j) {
13                 acc[i][j] = acc[i][j-1] + acc[i-1][j] - acc[i-1][j-1] + matrix[i-1][j-1];
14             }
15         }
16     }
17
18     int sumRegion(int row1, int col1, int row2, int col2) {
19         return acc[row2+1][col2+1] - acc[row1][col2+1] - acc[row2+1][col1] + acc[row1][col1];
20     }
21 };
22
23
24 // Your NumMatrix object will be instantiated and called as such:
25 // NumMatrix numMatrix(matrix);
26 // numMatrix.sumRegion(0, 1, 2, 3);
27 // numMatrix.sumRegion(1, 2, 3, 4);
时间: 2024-08-03 03:06:38

[LeetCode] Range Sum Query - Immutable & Range Sum Query 2D - Immutable的相关文章

【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 lower right corner (row2, col2). The above rectangle (w

304. Range Sum Query 2D - Immutable

/* * 304. Range Sum Query 2D - Immutable * 2016-7-3 by Mingyang * 这个题目自己做了一下同样的dp思路,但是out of boundry,为什么呢? * 因为自己老老实实的把dp的维度跟matrix的维度设成一样的了 * 那我们就把dp的维度再多设一个这样就很好地表示了 * dp[i][j]仅仅表示到i-1,j-1这个点(inclusive)的和的大小 */ class NumMatrix { private int[][] df;

304. Range Sum Query 2D - Immutable java solutions

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) is defined by (row1, col1) = (2, 1) and (row2, co

[Coding Made Simple] Sum Query in 2D Immutable Array

Given a 2D immutable array,  Write an efficient program to support any given sub-rectangle sum query in this 2D matrix. A simple solution is to add each entry inside the sub-rectangle. The runtime is O(n * m). The problem of this solution is that for

Segment Tree - Sum of given range

简单点说其实Segment Tree就是二分法的灵活运用. 需要的基础知识: 1 二分法 2 二叉树 3 最好熟悉堆排序 操作就是二分法和堆排序巧妙地合并起来. 有了这些基础知识Segment Tree就没有任何难度了. 参考原文: http://www.geeksforgeeks.org/segment-tree-set-1-sum-of-given-range/ 下面程序是一个类,现在只实现查找sum的操作. 注意: 1 如何在数组中定义树的跟和孩子节点,及如何计算 --  堆排序也需要的知

LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$

Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions: 0 < i, i + 1 < j, j + 1 < k < n - 1 Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should be

【leetcode刷题笔记】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t

[LeetCode] 034. Search for a Range (Medium) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Search for a Range (Medium) 链接: 题目:https://leetcode.com/problems/search-for-a-range/ 代码(github):https://github.com/illuz/leetcode 题意: 在有序数组中找到一个数的范围.(由于数

Leetcode刷题录之Two Sum

题意大概是给出一个数列num,和一个目标数target,然后要找出数列中的两个数,使得这两个数之和等于目标数,输出这两个数的下标值(从1开始算). 一个比较暴力的方法是用一个二重循环直接遍历序列,在第一重循环中找到a,在第二重循环中找到b,使得a+b=target,这种做法的时间复杂度是O(n^2), 提交时提示超时. 改进方法,先对数列num复制一个副本,然后对副本进行排序.在一重循环中找到a,接着对这个有序的副本进行二分查找,找到b= target-a,二分查找的 时间复杂度是O(logn)