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.

  1. class Solution {
  2. public:
  3. /**
  4. * @param matrix an integer matrix
  5. * @return the coordinate of the left-up and right-down number
  6. */
  7. vector<vector<int>> submatrixSum(vector<vector<int>>& m) {
  8.  
  9. int h = m.size();
  10. if(!h) return {};
  11. int w = m[].size();
  12. if(!w) return {};
  13.  
  14. // Get accumulate sum by columns
  15. vector<vector<int>> cols(h, vector<int>(w, ));
  16. for(int i = ; i < w; i ++)
  17. {
  18. unordered_map<int, int> rec; // sum-inx
  19. for(int j = ; j < h; j ++)
  20. {
  21. if(m[j][i] == )
  22. {
  23. return {{i, j},{i, j}};
  24. }
  25.  
  26. cols[j][i] = (j ? cols[j - ][i] : ) + m[j][i];
  27. if (!cols[j][i])
  28. {
  29. return {{, i}, {j, i}};
  30. }
  31. else if(rec.find(cols[j][i]) != rec.end())
  32. {
  33. return {{rec[cols[j][i]] + , i}, {j, i}};
  34. }
  35. rec[cols[j][i]] = j;
  36. }
  37. }
  38.  
  39. // horizontal case
  40. for(int i = ; i < h; i ++)
  41. for(int j = i; j < h; j ++)
  42. {
  43. vector<int> hsum(w, );
  44. for(int x = ; x < w; x ++)
  45. {
  46. int prev = ((i == ) ? : cols[i - ][x]);
  47. hsum[x] = cols[j][x] - prev;
  48. }
  49. //
  50. vector<int> asum(w, );
  51. unordered_map<int, int> rec; // sum-inx
  52. for(int x = ; x < w; x ++)
  53. {
  54. int nsum = (x ? asum[x - ] : ) + hsum[x];
  55. if (!nsum)
  56. {
  57. return {{i + , }, {j, x}};
  58. }
  59. else if(rec.find(nsum) != rec.end())
  60. {
  61. return {{i, rec[nsum] + }, {j, x}};
  62. }
  63. rec[nsum] = x;
  64. asum[x] = nsum;
  65. }
  66. }
  67.  
  68. return {};
  69. }
  70. };

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. 【转】How-To-Ask-Questions-The-Smart-Way

    提问的智慧 How To Ask Questions The Smart Way Copyright © 2001,2006,2014 Eric S. Raymond, Rick Moen 本指南英文 ...

  2. java项目上各种小问题

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

  3. DB2中的ROW_NUMBER() OVER()函数用法

      ROW_NUMBER() OVER()大概有俩方面的作用 1,分页, 并返回分页结果集.2,是对数据进行处理 分组 db2的分页: select tmp.* from ( SELECT rownu ...

  4. Codeforces Round #377 (Div. 2) A B C D 水/贪心/贪心/二分

    A. Buy a Shovel time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  5. jq中 load()方法 简介

    load()方法会在元素的onload事件中绑定一个处理函数.如果处理函数绑定给window对象,则会在所有内容(包括窗口,框架,对象和图像等)加载完毕后触发,如果处理函数绑定在元素上,则会在元素的内 ...

  6. html5的程序接口与元素变化

    除了原先的DOM接口,HTML5增加了更多API,如:1. 用于即时2D绘图的Canvas标签2. 定时媒体回放3. 离线数据库存储4.文档编辑5. 拖拽控制6. 浏览历史管理元素变化新的解析顺序新的 ...

  7. POJ 1488 Tex Quotes --- 水题

    POJ 1488 题目大意:给定一篇文章,将它的左引号转成 ``(1的左边),右引号转成 ''(两个 ' ) 解题思路:水题,设置一个bool变量标记是左引号还是右引号即可 /* POJ 1488 T ...

  8. memory CPU cache books

    http://www.amazon.com/Consistency-Coherence-Synthesis-Lectures-Architecture/dp/1608455645/ref=pd_sim ...

  9. 谓词的使用 -ios

    #import <Foundation/Foundation.h> @interface Person : NSObject<NSCopying> @property(nona ...

  10. apk反编译生成程序的源代码和图片、XML配置、语言资源等文件

    Android应用的UI越来越漂亮,遇到喜欢的我们可以通过反编译,得到应用的源代码借鉴下别人的思想. 具体步骤: 1.下载 apktool 下载地址:https://code.google.com/p ...