Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tags:
  [0,1,1,1]
]
<strong>输出:</strong>15
<strong>解释:</strong>
<strong>解释:</strong>
边长为 1 的正方形有 <strong>10</strong> 个。
边长为 2 的正方形有 <strong>4</strong> 个。
边长为 3 的正方形有 <strong>1</strong> 个。
Expand All @@ -42,15 +42,15 @@ tags:

<p><strong>示例 2:</strong></p>

<pre><strong>输入:</strong>matrix =
<pre><strong>输入:</strong>matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
<strong>输出:</strong>7
<strong>解释:</strong>
边长为 1 的正方形有 <strong>6</strong> 个。
边长为 1 的正方形有 <strong>6</strong> 个。
边长为 2 的正方形有 <strong>1</strong> 个。
正方形的总数 = 6 + 1 = <strong>7</strong>.
</pre>
Expand Down Expand Up @@ -172,6 +172,52 @@ func countSquares(matrix [][]int) int {
}
```

#### TypeScript

```ts
function countSquares(matrix: number[][]): number {
const [m, n] = [matrix.length, matrix[0].length];
const f = Array.from({ length: m }, () => Array(n));
const dfs = (i: number, j: number): number => {
if (i === m || j === n || !matrix[i][j]) return 0;
f[i][j] ??= 1 + Math.min(dfs(i + 1, j), dfs(i, j + 1), dfs(i + 1, j + 1));
return f[i][j];
};
let ans = 0;

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
ans += dfs(i, j);
}
}

return ans;
}
```

#### JavaScript

```js
function countSquares(matrix) {
const [m, n] = [matrix.length, matrix[0].length];
const f = Array.from({ length: m }, () => Array(n));
const dfs = (i, j) => {
if (i === m || j === n || !matrix[i][j]) return 0;
f[i][j] ??= 1 + Math.min(dfs(i + 1, j), dfs(i, j + 1), dfs(i + 1, j + 1));
return f[i][j];
};
let ans = 0;

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
ans += dfs(i, j);
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tags:
&nbsp; [0,1,1,1]
]
<strong>Output:</strong> 15
<strong>Explanation:</strong>
<strong>Explanation:</strong>
There are <strong>10</strong> squares of side 1.
There are <strong>4</strong> squares of side 2.
There is <strong>1</strong> square of side 3.
Expand All @@ -43,16 +43,16 @@ Total number of squares = 10 + 4 + 1 = <strong>15</strong>.
<p><strong class="example">Example 2:</strong></p>

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

Expand Down Expand Up @@ -172,6 +172,52 @@ func countSquares(matrix [][]int) int {
}
```

#### TypeScript

```ts
function countSquares(matrix: number[][]): number {
const [m, n] = [matrix.length, matrix[0].length];
const f = Array.from({ length: m }, () => Array(n));
const dfs = (i: number, j: number): number => {
if (i === m || j === n || !matrix[i][j]) return 0;
f[i][j] ??= 1 + Math.min(dfs(i + 1, j), dfs(i, j + 1), dfs(i + 1, j + 1));
return f[i][j];
};
let ans = 0;

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
ans += dfs(i, j);
}
}

return ans;
}
```

#### JavaScript

```js
function countSquares(matrix) {
const [m, n] = [matrix.length, matrix[0].length];
const f = Array.from({ length: m }, () => Array(n));
const dfs = (i, j) => {
if (i === m || j === n || !matrix[i][j]) return 0;
f[i][j] ??= 1 + Math.min(dfs(i + 1, j), dfs(i, j + 1), dfs(i + 1, j + 1));
return f[i][j];
};
let ans = 0;

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
ans += dfs(i, j);
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function countSquares(matrix) {
const [m, n] = [matrix.length, matrix[0].length];
const f = Array.from({ length: m }, () => Array(n));
const dfs = (i, j) => {
if (i === m || j === n || !matrix[i][j]) return 0;
f[i][j] ??= 1 + Math.min(dfs(i + 1, j), dfs(i, j + 1), dfs(i + 1, j + 1));
return f[i][j];
};
let ans = 0;

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
ans += dfs(i, j);
}
}

return ans;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function countSquares(matrix: number[][]): number {
const [m, n] = [matrix.length, matrix[0].length];
const f = Array.from({ length: m }, () => Array(n));
const dfs = (i: number, j: number): number => {
if (i === m || j === n || !matrix[i][j]) return 0;
f[i][j] ??= 1 + Math.min(dfs(i + 1, j), dfs(i, j + 1), dfs(i + 1, j + 1));
return f[i][j];
};
let ans = 0;

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
ans += dfs(i, j);
}
}

return ans;
}
Loading