-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountSquareSubmatricesWithAllOnes.cpp
More file actions
51 lines (48 loc) · 1.14 KB
/
countSquareSubmatricesWithAllOnes.cpp
File metadata and controls
51 lines (48 loc) · 1.14 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
// https://leetcode.com/problems/count-square-submatrices-with-all-ones/
class Solution
{
public:
int countSquares(std::vector<std::vector<int>> &matrix)
{
int result = 0;
int rowLength = matrix.size(); // 幾列
int colLength = matrix[0].size(); // 幾行
std::vector<std::vector<int>> dp = (std::vector<std::vector<int>>(
rowLength, std::vector<int>(colLength)));
// 處理第一列
for (int j = 0; j < colLength; j += 1)
{
if (matrix[0][j] == 1)
{
dp[0][j] = 1;
result += 1;
}
}
// 處理第一行
for (int i = 1; i < rowLength; i += 1)
{
if (matrix[i][0] == 1)
{
dp[i][0] = 1;
result += 1;
}
}
for (int i = 1; i < rowLength; i += 1)
{
for (int j = 1; j < colLength; j += 1)
{
if (matrix[i][j] == 1)
{
int minSide = std::min(
dp[i - 1][j - 1],
std::min(dp[i - 1][j], dp[i][j - 1]));
dp[i][j] = minSide + 1; // 記得加自己
result += dp[i][j];
}
}
}
return result;
}
};