Skip to content

Commit

Permalink
Feat: 54. Spiral Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
HC-kang committed Sep 19, 2024
1 parent 2190278 commit a33714f
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions spiral-matrix/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* https://leetcode.com/problems/spiral-matrix
* T.C. O(m * n)
* S.C. O(m * n)
*/
function spiralOrder(matrix: number[][]): number[] {
const clockwise = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
let currentDirection = 0;

const visited = new Array(matrix.length)
.fill(0)
.map(() => new Array(matrix[0].length).fill(false));

const result: number[] = [];

let row = 0;
let col = 0;

while (result.length < matrix.length * matrix[0].length) {
result.push(matrix[row][col]);
visited[row][col] = true;

const nextRow = row + clockwise[currentDirection][0];
const nextCol = col + clockwise[currentDirection][1];

if (
nextRow < 0 || nextRow >= matrix.length ||
nextCol < 0 || nextCol >= matrix[0].length ||
visited[nextRow][nextCol]
) {
currentDirection = (currentDirection + 1) % 4;
}

row += clockwise[currentDirection][0];
col += clockwise[currentDirection][1];
}

return result;
}

0 comments on commit a33714f

Please sign in to comment.