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.

Have you met this question in a real interview?

Yes
Example

Given matrix

[
[1 ,5 ,7],
[3 ,7 ,-8],
[4 ,-8 ,9],
]

return [(1,1), (2,2)]

Challenge

O(n3) time.

这道题跟LeetCode上的那道Max Sum of Rectangle No Larger Than K很类似。

解法一:

class Solution {
public:
/**
* @param matrix an integer matrix
* @return the coordinate of the left-up and right-down number
*/
vector<vector<int>> submatrixSum(vector<vector<int>>& matrix) {
if (matrix.empty() || matrix[].empty()) return {};
vector<vector<int>> sums = matrix;
int m = matrix.size(), n = matrix[].size();
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
int t = sums[i][j];
if (i > ) t += sums[i - ][j];
if (j > ) t += sums[i][j - ];
if (i > && j > ) t -= sums[i - ][j - ];
sums[i][j] = t;
for (int p = ; p <= i; ++p) {
for (int q = ; q <= j; ++q) {
int d = sums[i][j];
if (p > ) d -= sums[p - ][j];
if (q > ) d -= sums[i][q - ];
if (p > && q > ) d += sums[p - ][q - ];
if (d == ) return {{p, q}, {i, j}};
}
}
}
}
printVec(sums);
return {};
}
};

解法二:

class Solution {
public:
/**
* @param matrix an integer matrix
* @return the coordinate of the left-up and right-down number
*/
vector<vector<int>> submatrixSum(vector<vector<int>>& matrix) {
if (matrix.empty() || matrix[].empty()) return {};
int m = matrix.size(), n = matrix[].size();
for (int i = ; i < n; ++i) {
vector<int> sums(m, );
for (int j = i; j < n; ++j) {
for (int k = ; k < m; ++k) {
sums[k] += matrix[k][j];
}
int curSum = ;
unordered_map<int, int> map{{,-}};
for (int k = ; k < m; ++k) {
curSum += sums[k];
if (map.count(curSum)) return {{map[curSum] + , i}, {k, j}};
map[curSum] = k;
}
}
}
return {};
}
};

参考资料:

http://www.jiuzhang.com/solutions/submatrix-sum/

[LintCode] Submatrix Sum 子矩阵之和的更多相关文章

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

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

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

  3. poj 1050 To the Max(最大子矩阵之和,基础DP题)

    To the Max Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 38573Accepted: 20350 Descriptio ...

  4. array / matrix subarray/submatrix sum

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

  5. [LintCode] Two Sum 两数之和

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  6. poj 1050 To the Max(最大子矩阵之和)

    http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here  也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...

  7. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

  8. LintCode "4 Sum"

    4 Pointer solution. Key: when moving pointers, we skip duplicated ones. Ref: https://github.com/xbz/ ...

  9. lintcode:三数之和

    题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集 ...

随机推荐

  1. IplImage, CvMat, Mat 的关系

    IplImage, CvMat, Mat 的关系 转载来源:http://www.cnblogs.com/summerRQ/articles/2406109.html opencv中常见的与图像操作有 ...

  2. Ring3无敌进程让你的进程变得和smss.exe一样支持64

    本帖最后由 奋斗丶小Z 于 2016-6-6 13:39 编辑 此函数可以启用或关闭开启之后变得和系统进程一样被杀系统直接蓝屏系统进程也是此函数实现的上图 可以用于进程保护 <ignore_js ...

  3. 即时通讯(IM-instant messager)

    即时通讯又叫实时通讯,简单来说就是两个及以上的人使用网络进行文字.文件.语音和视频的交流. 首先,进行网络进行通信,肯定需要网络协议,即时通讯专用的协议就是xmpp.xmpp协议要传递的消息类型是xm ...

  4. JSON转javabean(pojo)利器

    别再对着json来手写javabean啦.这个工作完全不要脑子,而且耗时. 这里给大家提供三种方式: android studio版: 万能的插件:GsonFormat 如何安装? Preferenc ...

  5. SQL初级

    SQL是一个微软开发的数据库,因为联系到很多内部服务程序和文件所以安装和删除的时候有些人会遇上些麻烦,如果安装失败了那就得完全删除后重装,然而他自己自带的删除系统并不是那么给力,所以悲剧就诞生了,不行 ...

  6. Java学习——开端

    学号 <Java程序设计>第1周学习总结(1) 教材学习内容总结(第一章) Java最早是由Sun公司研发,原称Oak(橡树),开发者之一的James Gosling被尊称为Java之父. ...

  7. Docker使用-v挂载主机目录到容器后出现Permission denied

    1. 在挂载主机目录的到容器后,操作挂载的目录出现权限问题: # 将主机上的/data/share/master目录挂载到容器的/opt/share目录docker run -it --name=ma ...

  8. 寻找房间中心zz

    Finding the Centroid of a Room Boundary It's been a while since my last post and I'm sure most of yo ...

  9. Lc.exe已退出,代码为-1

    编译项目,出现提示"Lc.exe已退出,代码为-1" .   解决办法: 意思就是把licenses.licx这个文件里的内容删除,但是文件还在(此时是个空文件),发生这个问题的原 ...

  10. 在Revit中如何显示附件模块(Add Ins) 这个命令页?zz

      分类: 概念说明 Revit Revit界面编程 Revit 二次开发入门2013-08-22 13:58 1395人阅读 评论(9) 收藏 举报 在windows 7 32-bit OS 上装了 ...