Skip to content

Commit 549de73

Browse files
authored
Update README_EN.md
1 parent 4a49614 commit 549de73

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

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

0 commit comments

Comments
 (0)