Submatrix Sum
Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return the coordinate of the left-up and right-down number.
Given matrix
[
[1 ,5 ,7],
[3 ,7 ,-8],
[4 ,-8 ,9],
]
return [(1,1), (2,2)]
分析:
本质上还是subarray sum. 因为对于要找的那个submatrix, 一定在0 和 matrix.length 之间。假设那个submatrix的上下row分别为i 和 j,那么我们可以把从i到j的那部分矩阵从上到下加起来,这样组成了一个一维数组,然后用Subarray Sum的方法解就可以了。
public class Solution {
/**
* @param matrix an integer matrix
* @return the coordinate of the left-up and right-down number
*/
public int[][] submatrixSum(int[][] matrix) {
int[][] res = new int[][];
if (matrix == null || matrix.length == || matrix[].length == ) return res;
int m = matrix.length;
int n = matrix[].length;
// sum from 0,0 to i, j
int[][] sum = new int[m + ][n + ];
for (int i = ; i < sum.length; i++) {
for (int j = ; j < sum[].length; j++) {
sum[i][j] = matrix[i - ][j - ] + sum[i - ][j] + sum[i][j - ] - sum[i - ][j - ];
}
}
//如果那个为0的矩阵在row i 和 j,那么我们可以把从i到j的那部分矩阵从上到下加起来,这样组成了一个一维数组,然后用Subarray Sum的方法解就可以了
for (int r1 = ; r1 < m; r1++) {
for (int r2 = r1 + ; r2 <= m; r2++) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int j = ; j <= n; j++) {
int zeroToJSum = sum[r2][j] - sum[r1][j];
if (map.containsKey(zeroToJSum)) {
res[][] = r1;
res[][] = map.get(zeroToJSum);
res[][] = r2 - ;
res[][] = j - ;
return res;
} else {
map.put(zeroToJSum, j);
}
}
}
}
return res;
}
}
下面的代码思路和上面一样,只是实现不一样。
public class Solution {
/**
* @param matrix an integer matrix
* @return the coordinate of the left-up and right-down number
*/
public int[][] submatrixSum(int[][] matrix) {
int[][] res = new int[][];
if (matrix == null || matrix.length == || matrix[].length == ) return res;
int m = matrix.length;
int n = matrix[].length;
int[][] sum = new int[m][n];
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (i == ) {
sum[i][j] = matrix[i][j];
} else {
sum[i][j] = matrix[i][j] + sum[i - ][j];
}
}
}
for (int r1 = ; r1 < m; r1++) {
for (int r2 = r1; r2 < m; r2++) {
res = check(sum, r1, r2);
if (res != null) return res;
}
}
return res;
}
private int[][] check(int[][] sum, int r1, int r2) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(, -);
int zeroToJSum = ;
for (int j = ; j < sum[].length; j++) {
if (r1 == ) {
zeroToJSum = sum[r2][j] + zeroToJSum;
} else {
zeroToJSum = sum[r2][j] - sum[r1 - ][j] + zeroToJSum;
}
if (map.containsKey(zeroToJSum)) {
int[][] res = new int[][];
res[][] = r1;
res[][] = map.get(zeroToJSum) + ;
res[][] = r2;
res[][] = j;
return res;
} else {
map.put(zeroToJSum, j);
}
}
return null;
}
}
Reference:
https://segmentfault.com/a/1190000004878083
Submatrix Sum的更多相关文章
- array / matrix subarray/submatrix sum
Maximal Subarray Sum : O(n) scan-and-update dynamic programming, https://en.wikipedia.org/wiki/Maxim ...
- [LintCode] Submatrix Sum 子矩阵之和
Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...
- lintcode 中等题:Submatrix sum is 0 和为零的子矩阵
和为零的子矩阵 给定一个整数矩阵,请找出一个子矩阵,使得其数字之和等于0.输出答案时,请返回左上数字和右下数字的坐标. 样例 给定矩阵 [ [1 ,5 ,7], [3 ,7 ,-8], [4 ,-8 ...
- LintCode "Submatrix Sum"
Naive solution is O(n^4). But on 1 certain dimension, naive O(n^2) can be O(n) by this well-known eq ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- [CareerCup] 18.12 Largest Sum Submatrix 和最大的子矩阵
18.12 Given an NxN matrix of positive and negative integers, write code to find the submatrix with t ...
- Google - Largest Sum Submatrix
Given an NxN matrix of positive and negative integers, write code to find the submatrix with the lar ...
- Leetcode: Max Sum of Rectangle No Larger Than K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- Max Sub-matrix
Max Sub-matrix 教练找的题目,目前样列过了 题意:找子矩阵的最大周长 思路:先离散每列,再枚举列(n*n),在当前枚举的两列之间求每行的和(n*n*n),但是开两个数组,一个包含两列上的 ...
随机推荐
- 小组成员的github地址
袁颖https://github.com/joanyy/test 魏晓 https://github.com/weixiaohaobaobao/test 张晓磊 https://github.com/ ...
- Android的环境搭建
尽管以前并没有接触过软件开发.但是,现在网络资源实在是太丰富了.所以网搜了一下,认为Android的环境搭建可分为以下五个步骤来完成.第一步:安装JDK:第二步:配置Windows上JDK的变量环境: ...
- Smart Disk -- proposed by Liyuan Liu
Need 如今,照相渐渐得成为了人们的日常举动.几乎所有的人都在随时随地得照相.手机,相机,平板越来越多的设备对照相进行了支持,同时, 照片以一种前所未有的速度渐渐淹没我们的文件夹.而寻找照片,对照片 ...
- week8:个人博客作业
团队作业(5) 要求 在PM 带领下, 每个团队深入分析下面行业的App, 找到行业的Top 5 (从下面的三个备选中,任选一个行业即可) 英语学习/词典App 笔记App 旅游行业的手机App 选择 ...
- vs安装体验
鉴于vs都是英文,所以安装的时间实在是太长了,经过4个小时终于装完了. 首先要下载和安装Unit Test Generator.步骤为:tools->Extensions and Updates ...
- 2013长春网赛1004 hdu 4762 Cut the Cake
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4762 题意:有个蛋糕,切成m块,将n个草莓放在上面,问所有的草莓放在同一块蛋糕上面的概率是多少.2 & ...
- java8新特性(四)_Stream详解
之前写过一篇用stream处理map的文章,但是对stream没有一个整体的认识,这次结合并发编程网和ibm中介绍stream的文章进行一个总结,我会着重写对list的处理,毕竟实际工作中大家每天进行 ...
- [代码]--IIS发布网站浏览之后看到的是文件目录 & Internal Server Error 处理程序“ExtensionlessUrlHandler-ISAPI-4.0_64bit”在其模块列表中有一个错误模块“IsapiModule” 解决方法 & App_global.asax.pduxejp_.dll”--“拒绝访问。 ”
Q:IIS发布网站浏览之后看到的是文件目录 A:它出现了一个说到.NET4.0 更高框架什么的错误,所以我将 .NTE CRL版本由4.0改为2.0了,改为2.0后就出现了只能浏览文件目录了.改为4. ...
- C# 汉字排序
转载:http://blog.csdn.net/lgx040605112/article/details/6042316 using System; using System.Globalizatio ...
- Oracle中对number类型数据to_char()出现各位少0,或者值为###的处理
问题描述: 在Oracle中使用to_char()函数时当number值为小数时,常常个位0不显示 比如:select to_char(0.02) from dual,结果为.02 改进为 selec ...