和为零的子矩阵
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了和为零的子矩阵相关的知识,希望对你有一定的参考价值。
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 };
以上是关于和为零的子矩阵的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode刷题100天—1304. 和为零的N个唯一整数(数学)—day47
在MATLAB中,如何找出矩阵的非零元素。并且输出它所在的行和列。只要非零就为真。输出1。。