. [Swift] LeetCode1277 statistics are all square sub-matrix 1 | Count Square Submatrices with All Ones

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤ micro-channel public number: Shan Wing Chi ( let_us_code)
➤ bloggers domain: https://www.zengqiang.org
➤GitHub address: https://github.com/strengthen/LeetCode
➤ original address:
➤ If the address is not a link blog Park Yong Shan Chi, it may be crawling author of the article.
➤ text has been modified update! Click strongly recommended that the original address read! Support authors! Support the original!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.

 

Example 1:

Input: matrix =
[
  [0,1,1,1],
  [1,1,1,1],
  [0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:

Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
 

Constraints:

1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1


You give a m * n matrix, the matrix element of either 0 or 1, and return to where you count the number of square sub-matrix 1 is entirely composed.

 

Example 1:

Input: Matrix =
[
  [0,1,1,1],
  [1,1,1,1],
  [0,1,1,1]
]
Output: 15
Explanation:
a square of 1 with 10.
A square of 2 has four.
A square of 3 have a.
Number of squares = 10 + 4 + 1 = 15.
Example 2:

Input: Matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
a square of one of the six.
A square of a 2 there.
Number of squares = 6 + 1 = 7.
 

prompt:

1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1

Guess you like

Origin www.cnblogs.com/strengthen/p/12151547.html