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. (基础篇)PHP字符串函数

    1查找字符位置函数:   strpos($str,search,[int]):查找search在$str中的第一次位置从int开始: stripos($str,search,[int]):函数返回字符 ...

  2. ListView滚动到顶部

    videoAdapter.notifyDataSetChanged();videoListView.setSelection(0); 注意顺序先notify后setSelection

  3. java List 排序 Collections.sort()

    用Collections.sort方法对list排序有两种方法  第一种是list中的对象实现Comparable接口,如下: /** * 根据order对User排序 */ public class ...

  4. boot/setup.S

    !!    setup.S        Copyright (C) 1991, 1992 Linus Torvalds!! setup.s is responsible for getting th ...

  5. ASP.NET Web API与Rest web api:发布到IIS(二)(同发布.NET webservice)

    本文档大部分来源于:http://www.cnblogs.com/zqzjs/p/4705994.html 工具VS2010,window环境win7 一:Webservice的创建与方法查看调用 1 ...

  6. php部分---函数、四类常用函数、例子(下拉菜单添加内容);

    1.简单函数 四要素:返回类型,函数名,参数列表,函数体 function Show() { echo "hello"; } Show(); 2.有返回值的函数 function ...

  7. leetcode 128. Longest Consecutive Sequence ----- java

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  8. phpmyadmin使用中碰到的一些问题

    在导入数据库文件的时候出现 #1062 – Duplicate entry '1′ for key ‘PRIMARY' 说明在上一次的导入中没有完全导入,但是主键是自增的,所以要输入主键才能继续,解决 ...

  9. UVALive4287 hdu2767 hdu3836 强连通

    题意:有多个命题,需要证明他们可以互相推出,现在已经有一些证明关系即 A 可以证明 B,问至少还需要多少证明关系. 首先,如果某几个命题证明关系可以成环,那么这些命题必然可以相互证明,只要沿着环的边走 ...

  10. 排序算法总结(二)归并排序【Merge Sort】

    一.归并排序原理(Wikipedia) 归并排序本质是分治思想的应用,并且各层分治递归可以同时进行 1.申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列 2.设定两个指针,最初位置 ...