304. Range Sum Query 2D - Immutable
来源:互联网 发布:xp系统usb网络共享 编辑:程序博客网 时间:2024/06/05 00:29
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.
维护一个和数组sums[i][j]表示以(i,j)为右下角,(0,0)为左上角的矩形中所有数字的和。sum[i][j]可以通过DP来求。
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1] 1<=i<m , i<=j<n
求范围的和,借鉴的思想是平面直角坐标系中给定矩形的右上角、左下角,利用坐标系的关系求面积的思想。
long[][] sums;boolean bad=false;public NumMatrix(int[][] matrix){int m=matrix.length;if(m<1){bad=true;return ;}int n=matrix[0].length;if(n<1){bad=true;return ;}sums=new long[m][n];for(int i=0;i<m;i++)for(int j=0;j<n;j++){long sum=matrix[i][j];if(j>=1)sum+=sums[i][j-1];if(i>=1)sum+=sums[i-1][j];if(i>=1&&j>=1)sum-=sums[i-1][j-1];sums[i][j]=sum;}}public int sumRegion(int row1, int col1, int row2, int col2){if(bad)return 0;return (int)(sums[row2][col2]-(col1>=1?sums[row2][col1-1]:0)-(row1>=1?sums[row1-1][col2]:0)+((row1>=1&&col1>=1)?sums[row1-1][col1-1]:0));}
update 2016.07.29
边界如果要单独考虑的时候,不妨考虑平移一下再dp
class NumMatrix{long[][] dp;public NumMatrix(int[][] matrix){if(matrix.length<1||matrix[0].length<1)return ;int m=matrix.length;int n=matrix[0].length;dp=new long[m+1][n+1];for(int i=1;i<=m;i++)for(int j=1;j<=n;j++)dp[i][j]=matrix[i-1][j-1]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1];}public int sumRegion(int row1, int col1, int row2, int col2){return (int)(dp[row2+1][col2+1]-dp[row2+1][col1]-dp[row1][col2+1]+dp[row1][col1]);}}
0 0
- 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
- 304. Range Sum Query 2D - Immutable
- Range Sum Query 2D - Immutable
- Range Sum Query 2D - Immutable
- [Leetcode]Range Sum Query 2D - Immutable
- Range Sum Query 2D - Immutable
- Leetcode Range Sum Query 2D - Immutable
- LeetCode Range Sum Query 2D - Immutable
- [Leetcode]Range Sum Query 2D - Immutable
- 服务器维护手册
- 笨办法学Python之PowerShell入门篇
- RCNN + SPP + FAST RCNN + FASTER RCNN+ YOLO 论文总结
- ListCtrl的扩展类EditListCtrl
- Spring的集合注入
- 304. Range Sum Query 2D - Immutable
- 实现内存池MemoryPool(加了互斥锁的内存池实现:pthread_mutex_t + m_memory_pool)
- asp.net用input控件上传文件时Check错误文件路径的方法
- 数据库基础
- java微信分享config:invalid signature,自定义右上角菜单(只保留分享给好友和朋友圈)
- Handler消息传递机制
- Tyvj P1427 小白逛公园
- Binary Search Tree Iterator
- iic总线介绍