3333  ; [0,1,1,1]
3434]
3535<strong >输出:</strong >15
36- <strong >解释:</strong >
36+ <strong >解释:</strong >
3737边长为 1 的正方形有 <strong >10</strong > 个。
3838边长为 2 的正方形有 <strong >4</strong > 个。
3939边长为 3 的正方形有 <strong >1</strong > 个。
@@ -42,15 +42,15 @@ tags:
4242
4343<p ><strong >示例 2:</strong ></p >
4444
45- <pre ><strong >输入:</strong >matrix =
45+ <pre ><strong >输入:</strong >matrix =
4646[
4747 [1,0,1],
4848 [1,1,0],
4949 [1,1,0]
5050]
5151<strong >输出:</strong >7
5252<strong >解释:</strong >
53- 边长为 1 的正方形有 <strong >6</strong > 个。
53+ 边长为 1 的正方形有 <strong >6</strong > 个。
5454边长为 2 的正方形有 <strong >1</strong > 个。
5555正方形的总数 = 6 + 1 = <strong >7</strong >.
5656</pre >
7171
7272<!-- solution:start -->
7373
74- ### 方法一
74+ ### 方法一:动态规划
75+
76+ 我们定义 $f[ i] [ j ] $ 为以 $(i,j)$ 为右下角的正方形子矩阵的边长,初始时 $f[ i] [ j ] = 0$,答案为 $\sum_ {i,j} f[ i] [ j ] $。
77+
78+ 考虑 $f[ i] [ j ] $ 如何进行状态转移。
79+
80+ - 当 $\text{matrix}[ i] [ j ] = 0$ 时,有 $f[ i] [ j ] = 0$。
81+ - 当 $\text{matrix}[ i] [ j ] = 1$ 时,状态 $f[ i] [ j ] $ 的值取决于其上、左、左上三个位置的值:
82+ - 如果 $i = 0$ 或 $j = 0$,则 $f[ i] [ j ] = 1$。
83+ - 否则 $f[ i] [ j ] = \min(f[ i-1] [ j-1 ] , f[ i-1] [ j ] , f[ i] [ j-1 ] ) + 1$。
84+
85+ 答案为 $\sum_ {i,j} f[ i] [ j ] $。
86+
87+ 时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。
7588
7689<!-- tabs:start -->
7790
@@ -133,11 +146,14 @@ public:
133146 vector<vector<int >> f(m, vector<int >(n));
134147 for (int i = 0; i < m; ++i) {
135148 for (int j = 0; j < n; ++j) {
136- if (matrix[ i] [ j ] == 0) continue;
137- if (i == 0 || j == 0)
149+ if (matrix[ i] [ j ] == 0) {
150+ continue;
151+ }
152+ if (i == 0 || j == 0) {
138153 f[ i] [ j ] = 1;
139- else
154+ } else {
140155 f[ i] [ j ] = min(f[ i - 1] [ j - 1 ] , min(f[ i - 1] [ j ] , f[ i] [ j - 1 ] )) + 1;
156+ }
141157 ans += f[ i] [ j ] ;
142158 }
143159 }
@@ -176,45 +192,115 @@ func countSquares(matrix [][]int) int {
176192
177193``` ts
178194function countSquares(matrix : number [][]): number {
179- const [m, n] = [matrix .length , matrix [0 ].length ];
180- const f = Array .from ({ length: m }, () => Array (n ));
181- const dfs = (i : number , j : number ): number => {
182- if (i === m || j === n || ! matrix [i ][j ]) return 0 ;
183- f [i ][j ] ?? = 1 + Math .min (dfs (i + 1 , j ), dfs (i , j + 1 ), dfs (i + 1 , j + 1 ));
184- return f [i ][j ];
185- };
195+ const m = matrix .length ;
196+ const n = matrix [0 ].length ;
197+ const f: number [][] = Array .from ({ length: m }, () => Array (n ).fill (0 ));
186198 let ans = 0 ;
187199
188200 for (let i = 0 ; i < m ; i ++ ) {
189201 for (let j = 0 ; j < n ; j ++ ) {
190- ans += dfs (i , j );
202+ if (matrix [i ][j ] === 0 ) {
203+ continue ;
204+ }
205+ if (i === 0 || j === 0 ) {
206+ f [i ][j ] = 1 ;
207+ } else {
208+ f [i ][j ] = Math .min (f [i - 1 ][j - 1 ], Math .min (f [i - 1 ][j ], f [i ][j - 1 ])) + 1 ;
209+ }
210+ ans += f [i ][j ];
191211 }
192212 }
193213
194214 return ans ;
195215}
196216```
197217
218+ #### Rust
219+
220+ ``` rust
221+ impl Solution {
222+ pub fn count_squares (matrix : Vec <Vec <i32 >>) -> i32 {
223+ let m = matrix . len ();
224+ let n = matrix [0 ]. len ();
225+ let mut f = vec! [vec! [0 ; n ]; m ];
226+ let mut ans = 0 ;
227+
228+ for i in 0 .. m {
229+ for j in 0 .. n {
230+ if matrix [i ][j ] == 0 {
231+ continue ;
232+ }
233+ if i == 0 || j == 0 {
234+ f [i ][j ] = 1 ;
235+ } else {
236+ f [i ][j ] = std :: cmp :: min (f [i - 1 ][j - 1 ], std :: cmp :: min (f [i - 1 ][j ], f [i ][j - 1 ])) + 1 ;
237+ }
238+ ans += f [i ][j ];
239+ }
240+ }
241+
242+ ans
243+ }
244+ }
245+ ```
246+
198247#### JavaScript
199248
200249``` js
201- function countSquares ( matrix ) {
202- const [ m , n ] = [ matrix . length , matrix[ 0 ]. length ];
203- const f = Array . from ({ length : m }, () => Array (n));
204- const dfs = ( i , j ) => {
205- if (i === m || j === n || ! matrix[i][j]) return 0 ;
206- f[i][j] ?? = 1 + Math . min ( dfs (i + 1 , j), dfs (i, j + 1 ), dfs (i + 1 , j + 1 )) ;
207- return f[i][j] ;
208- } ;
250+ /**
251+ * @param {number[][]} matrix
252+ * @return {number}
253+ */
254+ var countSquares = function ( matrix ) {
255+ const m = matrix . length ;
256+ const n = matrix[ 0 ]. length ;
257+ const f = Array . from ({ length : m }, () => Array (n). fill ( 0 )) ;
209258 let ans = 0 ;
210259
211260 for (let i = 0 ; i < m; i++ ) {
212261 for (let j = 0 ; j < n; j++ ) {
213- ans += dfs (i, j);
262+ if (matrix[i][j] === 0 ) {
263+ continue ;
264+ }
265+ if (i === 0 || j === 0 ) {
266+ f[i][j] = 1 ;
267+ } else {
268+ f[i][j] = Math .min (f[i - 1 ][j - 1 ], Math .min (f[i - 1 ][j], f[i][j - 1 ])) + 1 ;
269+ }
270+ ans += f[i][j];
214271 }
215272 }
216273
217274 return ans;
275+ };
276+ ```
277+
278+ #### C#
279+
280+ ``` cs
281+ public class Solution {
282+ public int CountSquares (int [][] matrix ) {
283+ int m = matrix .Length ;
284+ int n = matrix [0 ].Length ;
285+ int [,] f = new int [m , n ];
286+ int ans = 0 ;
287+
288+ for (int i = 0 ; i < m ; i ++ ) {
289+ for (int j = 0 ; j < n ; j ++ ) {
290+ if (matrix [i ][j ] == 0 ) {
291+ continue ;
292+ }
293+ if (i == 0 || j == 0 ) {
294+ f [i , j ] = 1 ;
295+ } else {
296+ f [i , j ] = Math .Min (f [i - 1 , j - 1 ], Math .Min (f [i - 1 , j ], f [i , j - 1 ])) + 1 ;
297+ }
298+ ans += f [i , j ];
299+ }
300+ }
301+
302+ return ans ;
303+ }
218304}
219305```
220306
0 commit comments