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
69 changes: 47 additions & 22 deletions solution/0800-0899/0885.Spiral Matrix III/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,35 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int {
}
```

#### TypeScript

```ts
function spiralMatrixIII(rows: number, cols: number, rStart: number, cStart: number): number[][] {
// prettier-ignore
const dir = [[1,0],[0,1],[-1,0],[0,-1]]
let [x, y, i, size] = [cStart, rStart, 0, 0];
const ans: number[][] = [[y, x]];
const total = rows * cols;

while (ans.length < total) {
if (i % 2 === 0) size++;

for (let j = 0; ans.length < total && j < size; j++) {
x += dir[i][0];
y += dir[i][1];

if (0 <= x && x < cols && 0 <= y && y < rows) {
ans.push([y, x]);
}
}

i = (i + 1) % 4;
}

return ans;
}
```

#### JavaScript

```js
Expand All @@ -184,31 +213,27 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int {
* @return {number[][]}
*/
var spiralMatrixIII = function (rows, cols, rStart, cStart) {
const ans = [];
const totalCells = rows * cols;
const directions = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
let step = 0;
let d = 0;
let [r, c] = [rStart, cStart];
ans.push([r, c]);
while (ans.length < totalCells) {
if (d === 0 || d === 2) {
step++;
}
for (let i = 0; i < step; i++) {
r += directions[d][0];
c += directions[d][1];
if (r >= 0 && r < rows && c >= 0 && c < cols) {
ans.push([r, c]);
// prettier-ignore
const dir = [[1,0],[0,1],[-1,0],[0,-1]]
let [x, y, i, size] = [cStart, rStart, 0, 0];
const ans = [[y, x]];
const total = rows * cols;

while (ans.length < total) {
if (i % 2 === 0) size++;

for (let j = 0; ans.length < total && j < size; j++) {
x += dir[i][0];
y += dir[i][1];

if (0 <= x && x < cols && 0 <= y && y < rows) {
ans.push([y, x]);
}
}
d = (d + 1) % 4;

i = (i + 1) % 4;
}

return ans;
};
```
Expand Down
69 changes: 47 additions & 22 deletions solution/0800-0899/0885.Spiral Matrix III/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,35 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int {
}
```

#### TypeScript

```ts
function spiralMatrixIII(rows: number, cols: number, rStart: number, cStart: number): number[][] {
// prettier-ignore
const dir = [[1,0],[0,1],[-1,0],[0,-1]]
let [x, y, i, size] = [cStart, rStart, 0, 0];
const ans: number[][] = [[y, x]];
const total = rows * cols;

while (ans.length < total) {
if (i % 2 === 0) size++;

for (let j = 0; ans.length < total && j < size; j++) {
x += dir[i][0];
y += dir[i][1];

if (0 <= x && x < cols && 0 <= y && y < rows) {
ans.push([y, x]);
}
}

i = (i + 1) % 4;
}

return ans;
}
```

#### JavaScript

```js
Expand All @@ -180,31 +209,27 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int {
* @return {number[][]}
*/
var spiralMatrixIII = function (rows, cols, rStart, cStart) {
const ans = [];
const totalCells = rows * cols;
const directions = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
let step = 0;
let d = 0;
let [r, c] = [rStart, cStart];
ans.push([r, c]);
while (ans.length < totalCells) {
if (d === 0 || d === 2) {
step++;
}
for (let i = 0; i < step; i++) {
r += directions[d][0];
c += directions[d][1];
if (r >= 0 && r < rows && c >= 0 && c < cols) {
ans.push([r, c]);
// prettier-ignore
const dir = [[1,0],[0,1],[-1,0],[0,-1]]
let [x, y, i, size] = [cStart, rStart, 0, 0];
const ans = [[y, x]];
const total = rows * cols;

while (ans.length < total) {
if (i % 2 === 0) size++;

for (let j = 0; ans.length < total && j < size; j++) {
x += dir[i][0];
y += dir[i][1];

if (0 <= x && x < cols && 0 <= y && y < rows) {
ans.push([y, x]);
}
}
d = (d + 1) % 4;

i = (i + 1) % 4;
}

return ans;
};
```
Expand Down
40 changes: 18 additions & 22 deletions solution/0800-0899/0885.Spiral Matrix III/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,26 @@
* @return {number[][]}
*/
var spiralMatrixIII = function (rows, cols, rStart, cStart) {
const ans = [];
const totalCells = rows * cols;
const directions = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
let step = 0;
let d = 0;
let [r, c] = [rStart, cStart];
ans.push([r, c]);
while (ans.length < totalCells) {
if (d === 0 || d === 2) {
step++;
}
for (let i = 0; i < step; i++) {
r += directions[d][0];
c += directions[d][1];
if (r >= 0 && r < rows && c >= 0 && c < cols) {
ans.push([r, c]);
// prettier-ignore
const dir = [[1,0],[0,1],[-1,0],[0,-1]]
let [x, y, i, size] = [cStart, rStart, 0, 0];
const ans = [[y, x]];
const total = rows * cols;

while (ans.length < total) {
if (i % 2 === 0) size++;

for (let j = 0; ans.length < total && j < size; j++) {
x += dir[i][0];
y += dir[i][1];

if (0 <= x && x < cols && 0 <= y && y < rows) {
ans.push([y, x]);
}
}
d = (d + 1) % 4;

i = (i + 1) % 4;
}

return ans;
};
24 changes: 24 additions & 0 deletions solution/0800-0899/0885.Spiral Matrix III/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function spiralMatrixIII(rows: number, cols: number, rStart: number, cStart: number): number[][] {
// prettier-ignore
const dir = [[1,0],[0,1],[-1,0],[0,-1]]
let [x, y, i, size] = [cStart, rStart, 0, 0];
const ans: number[][] = [[y, x]];
const total = rows * cols;

while (ans.length < total) {
if (i % 2 === 0) size++;

for (let j = 0; ans.length < total && j < size; j++) {
x += dir[i][0];
y += dir[i][1];

if (0 <= x && x < cols && 0 <= y && y < rows) {
ans.push([y, x]);
}
}

i = (i + 1) % 4;
}

return ans;
}
Loading