[leetcode] 303. Range Sum Query && 304. Range Sum Query 2D - Immutable
2016-07-24 20:25
633 查看
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example:
Note:
You may assume that the array does not change.
There are many calls to sumRange function.
Subscribe to see which companies asked this question
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).
![](https://leetcode.com/static/images/courses/range_sum_query_2d.png)
The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.
Example:
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.
Subscribe to see which companies asked this question
Example:
Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3
Note:
You may assume that the array does not change.
There are many calls to sumRange function.
Subscribe to see which companies asked this question
class NumArray { public: NumArray(vector<int> &nums) { sums.push_back(0); for (int i = 0; i < nums.size(); i++) { sums.push_back(sums[i] + nums[i]); } } int sumRange(int i, int j) { return sums[j + 1] - sums[i]; } private: vector<int> sums; };
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).
![](https://leetcode.com/static/images/courses/range_sum_query_2d.png)
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.
Subscribe to see which companies asked this question
class NumMatrix { public: NumMatrix(vector<vector<int>> &matrix) { unsigned int nrow = matrix.size(); if (nrow == 0) return; unsigned int ncol = matrix[0].size(); for (int i = 0; i <= nrow; i++) { vector<int> row(ncol + 1, 0); matsum.push_back(row); } for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { matsum[i + 1][j + 1] = matsum[i][j + 1] + matsum[i + 1][j] - matsum[i][j] + matrix[i][j]; } } } int sumRegion(int row1, int col1, int row2, int col2) { return matsum[row2 + 1][col2 + 1] - matsum[row2 + 1][col1] - matsum[row1][col2 + 1] + matsum[row1][col1]; } private: vector<vector<int> > matsum; };
相关文章推荐
- 2016 UESTC Training for Data Structures C -> 卿学姐与诡异村庄 CDOJ 1328 并查集
- LeetCode[347] Top K Frequent Elements
- poj 2778 DNA Sequence
- LA 4287 Proving Equivalences(强连通分量缩点)
- Java多线程研究05-ThreadPoolExecutor中workQueue、threadFactory和handle
- Xcode工程目录下自动生成Build$DerivedData
- UE4拾取物体
- HDU-4027 Can you answer these queries?(线段树专题)
- POJ1458Common Subsequence(LCS)
- POJ 3080 Blue Jeans (很暴力)
- UVa 12100 Printer Queue(习题5-7)
- ABAP cl_gui_splitter_container屏幕分割
- saxbuilder用法
- poj 1458 Common Subsequence
- 探秘Java中String、StringBuilder以及StringBuffer
- 图形界面(GUI)
- std::unique (去重)
- poj 2533 Longest Ordered Subsequence
- burpsuite中intruder标签内attack type四种类型的用法和区别
- Binary Tree Longest Consecutive Sequence