Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5ac2738

Browse files
authored
feat: add solutions to lc problem: No.1277 (doocs#3678)
1 parent f3459fe commit 5ac2738

File tree

4 files changed

+136
-8
lines changed

4 files changed

+136
-8
lines changed

‎solution/1200-1299/1277.Count Square Submatrices with All Ones/README.md‎

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ tags:
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>
@@ -172,6 +172,52 @@ func countSquares(matrix [][]int) int {
172172
}
173173
```
174174

175+
#### TypeScript
176+
177+
```ts
178+
function 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+
};
186+
let ans = 0;
187+
188+
for (let i = 0; i < m; i++) {
189+
for (let j = 0; j < n; j++) {
190+
ans += dfs(i, j);
191+
}
192+
}
193+
194+
return ans;
195+
}
196+
```
197+
198+
#### JavaScript
199+
200+
```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+
};
209+
let ans = 0;
210+
211+
for (let i = 0; i < m; i++) {
212+
for (let j = 0; j < n; j++) {
213+
ans += dfs(i, j);
214+
}
215+
}
216+
217+
return ans;
218+
}
219+
```
220+
175221
<!-- tabs:end -->
176222
177223
<!-- solution:end -->

‎solution/1200-1299/1277.Count Square Submatrices with All Ones/README_EN.md‎

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ tags:
3333
&nbsp; [0,1,1,1]
3434
]
3535
<strong>Output:</strong> 15
36-
<strong>Explanation:</strong>
36+
<strong>Explanation:</strong>
3737
There are <strong>10</strong> squares of side 1.
3838
There are <strong>4</strong> squares of side 2.
3939
There is <strong>1</strong> square of side 3.
@@ -43,16 +43,16 @@ Total number of squares = 10 + 4 + 1 = <strong>15</strong>.
4343
<p><strong class="example">Example 2:</strong></p>
4444

4545
<pre>
46-
<strong>Input:</strong> matrix =
46+
<strong>Input:</strong> matrix =
4747
[
4848
[1,0,1],
4949
[1,1,0],
5050
[1,1,0]
5151
]
5252
<strong>Output:</strong> 7
53-
<strong>Explanation:</strong>
54-
There are <b>6</b> squares of side 1.
55-
There is <strong>1</strong> square of side 2.
53+
<strong>Explanation:</strong>
54+
There are <b>6</b> squares of side 1.
55+
There is <strong>1</strong> square of side 2.
5656
Total number of squares = 6 + 1 = <b>7</b>.
5757
</pre>
5858

@@ -172,6 +172,52 @@ func countSquares(matrix [][]int) int {
172172
}
173173
```
174174

175+
#### TypeScript
176+
177+
```ts
178+
function 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+
};
186+
let ans = 0;
187+
188+
for (let i = 0; i < m; i++) {
189+
for (let j = 0; j < n; j++) {
190+
ans += dfs(i, j);
191+
}
192+
}
193+
194+
return ans;
195+
}
196+
```
197+
198+
#### JavaScript
199+
200+
```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+
};
209+
let ans = 0;
210+
211+
for (let i = 0; i < m; i++) {
212+
for (let j = 0; j < n; j++) {
213+
ans += dfs(i, j);
214+
}
215+
}
216+
217+
return ans;
218+
}
219+
```
220+
175221
<!-- tabs:end -->
176222
177223
<!-- solution:end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function countSquares(matrix) {
2+
const [m, n] = [matrix.length, matrix[0].length];
3+
const f = Array.from({ length: m }, () => Array(n));
4+
const dfs = (i, j) => {
5+
if (i === m || j === n || !matrix[i][j]) return 0;
6+
f[i][j] ??= 1 + Math.min(dfs(i + 1, j), dfs(i, j + 1), dfs(i + 1, j + 1));
7+
return f[i][j];
8+
};
9+
let ans = 0;
10+
11+
for (let i = 0; i < m; i++) {
12+
for (let j = 0; j < n; j++) {
13+
ans += dfs(i, j);
14+
}
15+
}
16+
17+
return ans;
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function countSquares(matrix: number[][]): number {
2+
const [m, n] = [matrix.length, matrix[0].length];
3+
const f = Array.from({ length: m }, () => Array(n));
4+
const dfs = (i: number, j: number): number => {
5+
if (i === m || j === n || !matrix[i][j]) return 0;
6+
f[i][j] ??= 1 + Math.min(dfs(i + 1, j), dfs(i, j + 1), dfs(i + 1, j + 1));
7+
return f[i][j];
8+
};
9+
let ans = 0;
10+
11+
for (let i = 0; i < m; i++) {
12+
for (let j = 0; j < n; j++) {
13+
ans += dfs(i, j);
14+
}
15+
}
16+
17+
return ans;
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /