1. 程式人生 > >Linecode的做題答案

Linecode的做題答案

write size_t spa ret ++ 矩陣 turn end you

和為零的子矩陣

 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的做題答案