-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1074.cpp
35 lines (31 loc) · 1013 Bytes
/
1074.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <vector>
#include <algorithm>
#include <iostream>
#include <unordered_map>
using namespace std;
class Solution
{
public:
int numSubmatrixSumTarget(vector<vector<int>> &matrix, int target) {
int m = matrix.size(), n = matrix[0].size(), ret = 0;
vector<vector<int>> sum(m + 1, vector<int>(n + 1, 0));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
sum[i + 1][j + 1] = matrix[i][j] + sum[i + 1][j] + sum[i][j + 1] - sum[i][j];
}
}
unordered_map<int, int> hash;
for (int i = 0; i < m; ++i) {
for (int j = i + 1; j <= m; ++j) {
hash.clear();
hash.insert({0, 1});
for (int k = 0; k < n; ++k) {
int s = sum[j][k + 1] - sum[i][k + 1];
ret += hash.find(s - target) == hash.end() ? 0 : hash[s - target];
hash[s]++;
}
}
}
return ret;
}
};