leetecode 303|304. Range Sum Query
来源:互联网 发布:linq.js 对象 remove 编辑:程序博客网 时间:2024/06/03 21:30
303. 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) -> 1sumRange(2, 5) -> -1sumRange(0, 5) -> -3
Note:
- You may assume that the array does not change.
- There are many calls to sumRange function
sum[i] 存前i项和
class NumArray {public: NumArray(vector<int> nums) { int k = nums.size(); if (k == 0) return; sum.push_back(nums[0]); for (int i = 1; i < k ; i++) { sum.push_back(sum.back() + nums[i]); } } int sumRange(int i, int j) { if (i == 0) return sum[j]; return sum[j] - sum[i-1]; }private: vector<int> sum;};/** * Your NumArray object will be instantiated and called as such: * NumArray obj = new NumArray(nums); * int param_1 = obj.sumRange(i,j); */
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) 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) -> 8sumRegion(1, 1, 2, 2) -> 11sumRegion(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.
class NumMatrix {public: NumMatrix(vector<vector<int>> matrix) { int row = matrix.size(); if (row == 0) return; int col = matrix[0].size(); for (int j = 0; j < row; j++) sum.push_back(vector<int> (col, matrix[0][0])); for (int j = 1; j < col; j++) //第一行 sum[0][j] = sum[0][j-1] + matrix[0][j]; for (int i = 1; i < row; i++) //第一列 sum[i][0] = sum[i-1][0] + matrix[i][0]; for (int i = 1; i < row; i++) { for (int j = 1; j < col; j++) { sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i-1][j-1] + matrix[i][j]; } } for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) cout<<sum[i][j]<<" "; cout<<endl; } } int sumRegion(int row1, int col1, int row2, int col2) { int k1 = vaild(row2, col1 - 1); int k2 = vaild(row1 - 1, col2); int a1 = vaild(row1 - 1, col1 - 1); return sum[row2][col2] + a1 - k1 - k2; } int vaild(int i, int j) { int row = sum.size(); int col = sum[0].size(); if (i >= 0 && i < row && j >= 0 && j < col) return sum[i][j]; return 0; }private: vector<vector<int>> sum;};/** * Your NumMatrix object will be instantiated and called as such: * NumMatrix obj = new NumMatrix(matrix); * int param_1 = obj.sumRegion(row1,col1,row2,col2); */
阅读全文
0 0
- leetecode 303|304. Range Sum Query
- 303+304. Range Sum Query && Range Sum Query 2D
- 303,Range Sum Query
- 303 Range Sum Query
- [303]Range Sum Query - Immutable
- Leetcode 303 Range Sum Query
- Leetcode 303 Range Sum Query
- LeetCode 303 Range Sum Query
- 304. Range Sum Query 2D - Immutable
- 304. Range Sum Query 2D - Immutable
- 304. Range Sum Query 2D - Immutable
- 304. Range Sum Query 2D - Immutable
- 304. Range Sum Query 2D - Immutable
- 304. Range Sum Query 2D - Immutable
- 304. Range Sum Query 2D - Immutable
- 304. Range Sum Query 2D - Immutable
- 304. Range Sum Query 2D - Immutable**
- 304. Range Sum Query 2D - Immutable
- 没有对错,只是轮回
- Java 多线程
- TCP/IP,http,socket,长连接,短连接
- JS放在head和放在body中的区别
- EditText代码设置inputType后键盘输入类型变化问题
- leetecode 303|304. Range Sum Query
- Android之RxBus详解
- 技术分享连载(四十九)
- ue4 简单数据储存
- ios version规则
- 第6章 堆排序
- [tensorflow]argmax()函数
- Python使用技巧(环境,第三方库包等)(持续更新)
- 【数据结构】AVL树(未完)