Naive solution is O(n^4). But on 1 certain dimension, naive O(n^2) can be O(n) by this well-known equation: sum[i..j] = sum[0..j] - sum[0..i]. And pls take care of several corner cases.

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>>& m) { int h = m.size();
if(!h) return {};
int w = m[].size();
if(!w) return {}; // Get accumulate sum by columns
vector<vector<int>> cols(h, vector<int>(w, ));
for(int i = ; i < w; i ++)
{
unordered_map<int, int> rec; // sum-inx
for(int j = ; j < h; j ++)
{
if(m[j][i] == )
{
return {{i, j},{i, j}};
} cols[j][i] = (j ? cols[j - ][i] : ) + m[j][i];
if (!cols[j][i])
{
return {{, i}, {j, i}};
}
else if(rec.find(cols[j][i]) != rec.end())
{
return {{rec[cols[j][i]] + , i}, {j, i}};
}
rec[cols[j][i]] = j;
}
} // horizontal case
for(int i = ; i < h; i ++)
for(int j = i; j < h; j ++)
{
vector<int> hsum(w, );
for(int x = ; x < w; x ++)
{
int prev = ((i == ) ? : cols[i - ][x]);
hsum[x] = cols[j][x] - prev;
}
//
vector<int> asum(w, );
unordered_map<int, int> rec; // sum-inx
for(int x = ; x < w; x ++)
{
int nsum = (x ? asum[x - ] : ) + hsum[x];
if (!nsum)
{
return {{i + , }, {j, x}};
}
else if(rec.find(nsum) != rec.end())
{
return {{i, rec[nsum] + }, {j, x}};
}
rec[nsum] = x;
asum[x] = nsum;
}
} return {};
}
};

LintCode "Submatrix Sum"的更多相关文章

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

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

  2. array / matrix subarray/submatrix sum

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

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

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

  4. lintcode: k Sum 解题报告

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

  5. LintCode "4 Sum"

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

  6. Submatrix Sum

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

  7. Lintcode: Subarray Sum 解题报告

    Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...

  8. LintCode Subarray Sum

    For this problem we need to learn a new trick that if your start sum up all elements in an array. Wh ...

  9. [LintCode] Two Sum 两数之和

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

随机推荐

  1. CentOS安装Xen

    1.服务器环境及Xen版本: CentOS 5.4 64bit Xen-3.4.3,已经自带安装包 2.自制本地yum源: 安装httpd,指向本地xen yum源 3.修改yum.repo使其指向本 ...

  2. FreeBSD基金会添加新成员,梁莉成为第一位来自微软和中国的基金会董事

    这个月23日FreeBSD基金会很高兴地宣布Philip Paeps和Kylie Liang (梁莉)正式加入董事会. 梁莉,现任微软开源技术部高级项目经理,主要负责FreeBSD在公有云以及私有云的 ...

  3. #include #import @class 的一些用法区别

    从网上查了一些资料,整理了一下,发现很多都说的比较详尽,下面摘录自网络 说一下#import同class之间的区别 在ios中我们经常会在.h和.m中引入一些类啊等等一般用的是#import来进行声明 ...

  4. [翻译]深度学习的机器(The learning machines)

    学习的机器 用大量的数据识别图像和语音,深度学习的计算机(deep-learning computers) 向真正意义上的人工智能迈出了一大步. Nicola Jones Computer Scien ...

  5. Ionic基础——侧边栏ion-side-menus 以及ion-tap结合侧边栏详解

    一. 侧边栏菜单 : ion-side-menus 侧边栏菜单是一个最多包含三个子容器的元素: 默认情况下,侧边栏菜单将只显示ion-side-menu-content容器的内容. 向左滑动时,将显示 ...

  6. Dreamweaver_CS6安装与破解,手把手教程

    Dreamweaver_CS6安装与破解,手把手教程 | 浏览:11495 | 更新:2015-12-31 10:28 1 2 3 4 5 6 7 分步阅读 Adobe Dreamweaver是一款非 ...

  7. jQuery中的ajax服务端返回方式详细说明

    http://blog.sina.com.cn/s/blog_6f92e3a70100u3b6.html     上次总结了下ajax的所有参数项,其中有一项dataType是设置具体的服务器返回方式 ...

  8. JavaScript学习记录总结(十)——几个重要的BOM对象

    一.弹出框 <script type="text/javascript">    window.onload=function(){         window.al ...

  9. MySQL复制的基本概念和实现

    MySQL的复制的概念是完成水平扩展的架构 MySQL性能方面的扩展方式有scale on(向上扩展,垂直扩展)                          scale out(向外扩展,水平扩 ...

  10. 三星Mega 6.3(i9200)删除kingroot

    关于kingroot实际效果怎么样,是不是流氓软件,我不做评论. 手机型号,三星galaxy mega 6.3(i9200)水货,更详细的机型不知道怎么看. 刷了官方的rom以后,下载了kingroo ...