首页 > 代码库 > Linecode的做题答案

Linecode的做题答案

和为零的子矩阵

 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>>& matrix) {
 8     // Write your code here
 9     vector<vector<int>> a;
10     int y = matrix.size();
11     int x = matrix[0].size();
12     for (int yy = 1; yy != y + 1; yy++) { //1到y层的阶
13         for (int xx = 1; xx != x + 1; xx++) { //1到x层的阶
14             int n = (x - xx + 1)*(y - yy + 1);
15             int row = -1;
16             for (int w = 0; w != n; w++) { //此阶的所有遍历次数
17                 if (row == x - xx)
18                     row = -1;
19                 row++;
20                 int sum = 0;
21                 int s = -1;
22                 for (size_t first = w/(x - xx + 1); first != w/(x - xx + 1) + yy; first++) { //外左循环
23                     s++;
24                     int ss = -1;
25                     for (size_t seconds = row; seconds != row + xx; seconds++) { //内循环
26                         ss++;
27                         sum = sum + matrix[first][seconds];
28                         if (s == 0 && ss == 0) {
29                             a.push_back({first, seconds});
30                         }
31                         if (s == yy -1 && ss == xx -1 && sum == 0) {
32                             a.push_back({first, seconds});
33                             return a;
34                         }
35 
36                     }
37                 }
38                 a.erase(--a.end());
39             }
40         }
41     }
42 }
43 };

 

Linecode的做题答案