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.

Example

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的更多相关文章

  1. array / matrix subarray/submatrix sum

    Maximal Subarray Sum : O(n) scan-and-update dynamic programming, https://en.wikipedia.org/wiki/Maxim ...

  2. [LintCode] Submatrix Sum 子矩阵之和

    Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...

  3. lintcode 中等题:Submatrix sum is 0 和为零的子矩阵

    和为零的子矩阵 给定一个整数矩阵,请找出一个子矩阵,使得其数字之和等于0.输出答案时,请返回左上数字和右下数字的坐标. 样例 给定矩阵 [ [1 ,5 ,7], [3 ,7 ,-8], [4 ,-8 ...

  4. 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 ...

  5. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  6. [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 ...

  7. Google - Largest Sum Submatrix

    Given an NxN matrix of positive and negative integers, write code to find the submatrix with the lar ...

  8. 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 ...

  9. Max Sub-matrix

    Max Sub-matrix 教练找的题目,目前样列过了 题意:找子矩阵的最大周长 思路:先离散每列,再枚举列(n*n),在当前枚举的两列之间求每行的和(n*n*n),但是开两个数组,一个包含两列上的 ...

随机推荐

  1. 第二个Sprint冲刺总结

    第二个Sprint冲刺总结 ( 1)团队Github: https://github.com/ouqifeng/EasyGoOperation.git ( 2 ) 团队贡献分: 廖焯燊:22 何武鹏: ...

  2. Python之路3【知识点】白话Python编码和文件操作(截载)

    无意发现这篇文章讲的比较好,存下来供参考: http://www.cnblogs.com/luotianshuai/p/5735051.html

  3. 常用的cpl 命令 运行直接打开控制台的简单方法

    转载百度百科   工作中处理 windows机器 有时候 打开 网路修改ip地址特别繁琐,所以找了下 快速打开一些简单的控制台 能提高工作效率.   (Control Panel extension) ...

  4. [转帖]Mysql 开启跟踪的一个方法

    MySQL 事件跟踪器 , MySQL 无须重启服务 跟踪 SQL , 也无须配置日志 原博客地址: https://www.cnblogs.com/wuyifu/p/3328024.html 第一步 ...

  5. linux下&、nohup与screen的比较

    & 首先,linux进程是区分前台进程和后台进程的. 通常,在终端输入的命令执行的前台进程模式.如果一个命令要执行好久,就会阻塞住终端好久,不能进行其他工作,所以,我们可以把执行花费时间很长的 ...

  6. 基于Vue-cli 快速搭建项目

    Vue-cli可以快速帮助我们创建一个项目,这是官方给我们提供的脚手架.下面我说一下vue-cli的使用方法. 一.准备工作 在使用vue-cli时,首先需要安装nodejs,npm,其次需全局安装v ...

  7. HUAS 2018暑假第一周比赛-题解

    小朋友们有问题评论区 :) B. 子串计算 难度系数 : ☆ Main idea : 模拟 暴力 按照题目的要求一步一步来就行了 之所以可行的原因是从左往右扫,如果扫到一个子串,把它删除掉之后,假设当 ...

  8. c++ std::function

    std::function 是一个模板类,用于封装各种类似于函数这样的对象,例如普通函数,仿函数,匿名函数等等.其强大的多态能力,让其使用只依赖于调用特征.在程序的升级中,可以实现一个调用表,以兼容新 ...

  9. MySQL 主从复制详解

    读写分离的意思是,写入的时候向 a 服务器写入,而读出的时候从 b c d 甚至更多的服务器读出:这样的架构适合于读多写少的应用,最典型的就是火车购票系统,一般我们买票的时候要先查询好多次,包括车次啊 ...

  10. [LOJ6436][PKUSC2018]神仙的游戏

    loj description 给你一个只有01和?的字符串,问你是否存在一种把?改成01的方案使串存在一个长度为\(1-n\)的\(border\).\(n\le5\times10^5\) sol ...