forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1074.cpp
30 lines (29 loc) · 765 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
class Solution
{
public:
int numSubmatrixSumTarget(vector<vector<int>>& A, int target)
{
int r = A.size(), c = A[0].size();
for (auto& row : A)
{
for (int i = 1; i < c; i++) row[i] += row[i - 1];
}
int res = 0, cur = 0;
unordered_map<int, int> cnt;
for (int i = 0; i < c; i++)
{
for (int j = i; j < c; j++)
{
cnt.clear();
cnt[0] = 1, cur = 0;
for (int k = 0; k < r; k++)
{
cur += A[k][j] - (i ? A[k][i - 1] : 0);
res += cnt[cur - target];
cnt[cur]++;
}
}
}
return res;
}
};