Spiral Matrix Order
Medium matrix · simulation
Description
Constraints
1 <= rows, cols <= 100
Examples
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]
Hints (1)
- Maintain top, bottom, left and right boundaries and shrink them.
Solutions
Shrinking boundaries. · Time O(rows*cols) · Space O(1)
func spiralOrder(matrix [][]int) []int {
out := []int{}
top, bottom, left, right := 0, len(matrix)-1, 0, len(matrix[0])-1
for top <= bottom && left <= right {
for c := left; c <= right; c++ {
out = append(out, matrix[top][c])
}
top++
for r := top; r <= bottom; r++ {
out = append(out, matrix[r][right])
}
right--
if top <= bottom {
for c := right; c >= left; c-- {
out = append(out, matrix[bottom][c])
}
bottom--
}
if left <= right {
for r := bottom; r >= top; r-- {
out = append(out, matrix[r][left])
}
left++
}
}
return out
}
Shrinking boundaries. · Time O(rows*cols) · Space O(1)
function spiralOrder(matrix) {
const out = [];
let top = 0, bottom = matrix.length - 1, left = 0, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
for (let c = left; c <= right; c++) out.push(matrix[top][c]);
top++;
for (let r = top; r <= bottom; r++) out.push(matrix[r][right]);
right--;
if (top <= bottom) { for (let c = right; c >= left; c--) out.push(matrix[bottom][c]); bottom--; }
if (left <= right) { for (let r = bottom; r >= top; r--) out.push(matrix[r][left]); left++; }
}
return out;
}
Shrinking boundaries. · Time O(rows*cols) · Space O(1)
function spiralOrder($matrix) {
$out = [];
$top = 0; $bottom = count($matrix) - 1; $left = 0; $right = count($matrix[0]) - 1;
while ($top <= $bottom && $left <= $right) {
for ($c = $left; $c <= $right; $c++) { $out[] = $matrix[$top][$c]; }
$top++;
for ($r = $top; $r <= $bottom; $r++) { $out[] = $matrix[$r][$right]; }
$right--;
if ($top <= $bottom) { for ($c = $right; $c >= $left; $c--) { $out[] = $matrix[$bottom][$c]; } $bottom--; }
if ($left <= $right) { for ($r = $bottom; $r >= $top; $r--) { $out[] = $matrix[$r][$left]; } $left++; }
}
return $out;
}
Shrinking boundaries. · Time O(rows*cols) · Space O(1)
def spiralOrder(matrix):
out = []
top, bottom, left, right = 0, len(matrix) - 1, 0, len(matrix[0]) - 1
while top <= bottom and left <= right:
for c in range(left, right + 1):
out.append(matrix[top][c])
top += 1
for r in range(top, bottom + 1):
out.append(matrix[r][right])
right -= 1
if top <= bottom:
for c in range(right, left - 1, -1):
out.append(matrix[bottom][c])
bottom -= 1
if left <= right:
for r in range(bottom, top - 1, -1):
out.append(matrix[r][left])
left += 1
return out
Shrinking boundaries. · Time O(rows*cols) · Space O(1)
function spiralOrder(matrix: number[][]): number[] {
const out: number[] = [];
let top = 0, bottom = matrix.length - 1, left = 0, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
for (let c = left; c <= right; c++) out.push(matrix[top][c]);
top++;
for (let r = top; r <= bottom; r++) out.push(matrix[r][right]);
right--;
if (top <= bottom) { for (let c = right; c >= left; c--) out.push(matrix[bottom][c]); bottom--; }
if (left <= right) { for (let r = bottom; r >= top; r--) out.push(matrix[r][left]); left++; }
}
return out;
}
Create a free account to submit solutions, track progress and save problems.
Create account