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 20e15cd

Browse files
feat: add solutions to lc problem: No.0885 (doocs#3380)
1 parent 07bc708 commit 20e15cd

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

‎solution/0800-0899/0885.Spiral Matrix III/README.md‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,46 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int {
173173
}
174174
```
175175

176+
#### JavaScript
177+
178+
```js
179+
/**
180+
* @param {number} rows
181+
* @param {number} cols
182+
* @param {number} rStart
183+
* @param {number} cStart
184+
* @return {number[][]}
185+
*/
186+
var spiralMatrixIII = function (rows, cols, rStart, cStart) {
187+
const ans = [];
188+
const totalCells = rows * cols;
189+
const directions = [
190+
[0, 1],
191+
[1, 0],
192+
[0, -1],
193+
[-1, 0],
194+
];
195+
let step = 0;
196+
let d = 0;
197+
let [r, c] = [rStart, cStart];
198+
ans.push([r, c]);
199+
while (ans.length < totalCells) {
200+
if (d === 0 || d === 2) {
201+
step++;
202+
}
203+
for (let i = 0; i < step; i++) {
204+
r += directions[d][0];
205+
c += directions[d][1];
206+
if (r >= 0 && r < rows && c >= 0 && c < cols) {
207+
ans.push([r, c]);
208+
}
209+
}
210+
d = (d + 1) % 4;
211+
}
212+
return ans;
213+
};
214+
```
215+
176216
<!-- tabs:end -->
177217

178218
<!-- solution:end -->

‎solution/0800-0899/0885.Spiral Matrix III/README_EN.md‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,46 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int {
169169
}
170170
```
171171

172+
#### JavaScript
173+
174+
```js
175+
/**
176+
* @param {number} rows
177+
* @param {number} cols
178+
* @param {number} rStart
179+
* @param {number} cStart
180+
* @return {number[][]}
181+
*/
182+
var spiralMatrixIII = function (rows, cols, rStart, cStart) {
183+
const ans = [];
184+
const totalCells = rows * cols;
185+
const directions = [
186+
[0, 1],
187+
[1, 0],
188+
[0, -1],
189+
[-1, 0],
190+
];
191+
let step = 0;
192+
let d = 0;
193+
let [r, c] = [rStart, cStart];
194+
ans.push([r, c]);
195+
while (ans.length < totalCells) {
196+
if (d === 0 || d === 2) {
197+
step++;
198+
}
199+
for (let i = 0; i < step; i++) {
200+
r += directions[d][0];
201+
c += directions[d][1];
202+
if (r >= 0 && r < rows && c >= 0 && c < cols) {
203+
ans.push([r, c]);
204+
}
205+
}
206+
d = (d + 1) % 4;
207+
}
208+
return ans;
209+
};
210+
```
211+
172212
<!-- tabs:end -->
173213

174214
<!-- solution:end -->
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {number} rows
3+
* @param {number} cols
4+
* @param {number} rStart
5+
* @param {number} cStart
6+
* @return {number[][]}
7+
*/
8+
var spiralMatrixIII = function (rows, cols, rStart, cStart) {
9+
const ans = [];
10+
const totalCells = rows * cols;
11+
const directions = [
12+
[0, 1],
13+
[1, 0],
14+
[0, -1],
15+
[-1, 0],
16+
];
17+
let step = 0;
18+
let d = 0;
19+
let [r, c] = [rStart, cStart];
20+
ans.push([r, c]);
21+
while (ans.length < totalCells) {
22+
if (d === 0 || d === 2) {
23+
step++;
24+
}
25+
for (let i = 0; i < step; i++) {
26+
r += directions[d][0];
27+
c += directions[d][1];
28+
if (r >= 0 && r < rows && c >= 0 && c < cols) {
29+
ans.push([r, c]);
30+
}
31+
}
32+
d = (d + 1) % 4;
33+
}
34+
return ans;
35+
};

0 commit comments

Comments
(0)

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