leetcode 304. Range Sum Query 2D - Immutable
2016-05-13 13:17
387 查看
传送门
QuestionEditorial Solution
Total Accepted: 12544 Total Submissions: 56503 Difficulty: Medium
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:
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
Hide Tags
Dynamic Programming
Show Similar Problems
思路:动态规划
04. Range Sum Query 2D - Immutable
My SubmissionsQuestionEditorial Solution
Total Accepted: 12544 Total Submissions: 56503 Difficulty: Medium
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:
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
Hide Tags
Dynamic Programming
Show Similar Problems
Submission Details
12 / 12 test cases passed. | Status:Accepted |
Runtime: 24 ms |
class NumMatrix { public: NumMatrix(vector<vector<int>> &matrix) { if(matrix.empty()){ return; } int n = matrix.size(), m = matrix[0].size(); sums = vector<vector<int>>(n+1, vector<int>(m+1, 0)); int i,j; sums[0][0] = matrix[0][0]; for(i = 1;i <= n;i++){ for(j = 1;j <= m;j++){ sums[i][j] = sums[i - 1][j] + sums[i][j - 1] - sums[i - 1][j - 1] + matrix[i -1][j - 1]; } } } 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 numMatrix(matrix); // numMatrix.sumRegion(0, 1, 2, 3); // numMatrix.sumRegion(1, 2, 3, 4);
相关文章推荐
- IOS:FMDB使用databaseQueue实现数据库操作线程安全
- vim E437: terminal capability "cm" required
- Logging only once for UICollectionViewFlowLayout cache mismatched frame
- NSURLRequest各种缓存方式讲解
- iOS之UIScrollView实现无限滚动,思路与代码
- Qt中Ui名字空间以及setupUi函数的原理和实现
- android常用UI控件总结
- MySQL提示:The server quit without updating PID file问题的解决办法
- Java并发编程之2——同步工具类的使用(CountDownLatch,CyclicBarrier,BlockungQueue,Semaphore)
- Android工作杂项总结(APN,3G,i2c,SystemUI等)
- LeetCode---Implement Queue Using Stacks、Implement Stack Using Queues解题分析
- UITableView-01初见
- Sequelize 事务大并发下造成的死锁问题。
- java execute、executeQuery和executeUpdate之间的区别
- UITextField常用属性及键盘收回的几种方式
- UE中使用正则表达式的一些技巧
- 【源代码】UITextView使用自定义微博表情键盘
- UI ID UE GUI 是什么?
- iOS获取UUID,并使用keychain存储
- UGUI batch 规则和性能优化