Skip to content

Commit 4a49614

Browse files
authored
Update README.md
1 parent a759f9c commit 4a49614

File tree

1 file changed

+40
-0
lines changed
  • solution/0800-0899/0885.Spiral Matrix III

1 file changed

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

0 commit comments

Comments
 (0)