1074. Number of Submatrices That Sum to Target 發表於 2024-01-28 | 分類於 leetcode problemsolution123456789101112131415161718192021222324252627282930313233343536373839class Solution {public: int numSubmatrixSumTarget(vector<vector<int>>& matrix, int target) { int n = matrix.size(), m = matrix[0].size(); // 0 1 1 // 1 3 4 // 1 4 5 vector<vector<int>> presum = vector<vector<int>>(n+1, vector<int>(m+1,0)); for(int i=1;i<=n;++i) { for(int j=1;j<=m;++j) { presum[i][j] = matrix[i-1][j-1] + presum[i-1][j] + presum[i][j-1]- presum[i-1][j-1]; } } int ret = 0; for(int i=1;i<=n;++i) { cout<<endl; for(int j = 1;j<=m;++j) { for(int x = 0;x<i ; ++x) { for(int y = 0;y<j ; y++) { int temp = presum[i][j] - presum[x][j] - presum[i][y] + presum[x][y] ; if(temp == target) { ret++; } } } } } return ret; }}; analysis time complexity O(nnmm) space complexity O(nm)