forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
function spiralOrder(matrix: number[][]): number[] { | ||
const res: number[] = []; | ||
let left = 0; | ||
let right = matrix[0].length; | ||
let top = 0; | ||
let bottom = matrix.length; | ||
|
||
while (left < right && top < bottom) { | ||
// get every i in the top row | ||
for (let i = left; i < right; i++) { | ||
res.push(matrix[top][i]); | ||
} | ||
top += 1; | ||
|
||
// get every i in the right column | ||
for (let i = top; i < bottom; i++) { | ||
res.push(matrix[i][right - 1]); | ||
} | ||
right -= 1; | ||
|
||
if (!(left < right && top < bottom)) { | ||
break; | ||
} | ||
|
||
// get ever i in the bottom row | ||
for (let i = right - 1; i > left - 1; i--) { | ||
res.push(matrix[bottom - 1][i]); | ||
} | ||
bottom -= 1; | ||
|
||
// get evet i in the left column | ||
for (let i = bottom - 1; i > top - 1; i--) { | ||
res.push(matrix[i][left]); | ||
} | ||
left += 1; | ||
} | ||
|
||
return res; | ||
} |