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. Qt之添加Windows资源文件(.rc文件)

    简述 在Windows下使用Qt时,通常会用到Windows的资源文件 - 为exe设置信息,其中包括:文件说明.产品名称.产品版本.版权等信息... 由于是Windows平台相关的东西,Qt助手中对 ...

  2. java项目上各种小问题

    md,出现好几次这种问题,还得上百度? 以此为证,再出现这种问题我就不信想不起来怎么解决!!!----清除不存在jar包 ok,第二个问题: 每个类上有无数个红叉,然而代码并没有问题 解决方案:run ...

  3. Sed 直接修改文件

    sed最常用的用法莫过于替换文件,然而其默认的模式是直接输出在shell中 sed 's/Old/New/' My_File.txt 如果我们想要sed直接在文件中更改,只需要在sed后面添加 -i ...

  4. Java-->分割文件

    --> 通过IO 流将一个文件分割成多份 package com.dragon.java.splitfile; import java.io.File; import java.io.FileI ...

  5. 4.2springmvc校验

    1.hibernate的校验框架validation所需要jar包: 2 在applicationContext.xml中配置校验器: <!-- 校验器 --> <bean id=& ...

  6. Codeforces Round #147 (Div. 2)

    A. Free Cash 判断值相同的最长长度. B. Young Table 按从上到下,从左到右排序,每个位置最多交换一次. C. Primes on Interval \(p_i\)表示位置\( ...

  7. HZAU 17:LCS

    17: LCS Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 184  Solved: 43[Submit][Status][Web Board] De ...

  8. IOS中使用手机号注册

    #import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface KCVVerify : NSObject ...

  9. 附录二 C语言标准库

    上章回顾 数组和指针相同与不同 通过指针访问数组和通过数组访问指针 指针在什么时候可以加减运算 函数指针的申明和调用 函数数组和数组函数 git@github.com:Kevin-Dfg/Data-S ...

  10. Android开源框架ImageLoader的完美例子

    本文转载于:http://blog.csdn.net/wwj_748/article/details/10079311 2013年8月19日开源框架之Universal_Image_Loader学习 ...