#include <stdlib.h>
int *spiralOrder(int **matrix, int matrixSize, int *matrixColSize,
int *returnSize) {
int top = 0, bottom = matrixSize - 1;
int left = 0, right = matrixColSize[0] - 1;
int *res = malloc(sizeof(int) * matrixSize * matrixColSize[0]);
int k = 0;
while (top <= bottom && left <= right) {
for (int i = left; i <= right; i++)
res[k++] = matrix[top][i];
top++;
for (int i = top; i <= bottom; i++)
res[k++] = matrix[i][right];
right--;
if (top <= bottom) {
for (int i = right; i >= left; i--)
res[k++] = matrix[bottom][i];
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--)
res[k++] = matrix[i][left];
left++;
}
}
*returnSize = k;
return res;
}
#include <vector>
using namespace std;
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>> &matrix) {
int top = 0, bottom = matrix.size() - 1;
int left = 0, right = matrix[0].size() - 1;
vector<int> res;
while (top <= bottom && left <= right) {
for (int i = left; i <= right; i++)
res.push_back(matrix[top][i]);
top++;
for (int i = top; i <= bottom; i++)
res.push_back(matrix[i][right]);
right--;
if (top <= bottom) {
for (int i = right; i >= left; i--)
res.push_back(matrix[bottom][i]);
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--)
res.push_back(matrix[i][left]);
left++;
}
}
return res;
}
};
package mathgeometry
func spiralOrder(matrix [][]int) []int {
top, bottom := 0, len(matrix)-1
left, right := 0, len(matrix[0])-1
res := make([]int, 0, len(matrix)*len(matrix[0]))
for top <= bottom && left <= right {
for i := left; i <= right; i++ {
res = append(res, matrix[top][i])
}
top++
for i := top; i <= bottom; i++ {
res = append(res, matrix[i][right])
}
right--
if top <= bottom {
for i := right; i >= left; i-- {
res = append(res, matrix[bottom][i])
}
bottom--
}
if left <= right {
for i := bottom; i >= top; i-- {
res = append(res, matrix[i][left])
}
left++
}
}
return res
}
import java.util.ArrayList;
import java.util.List;
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int top = 0, bottom = matrix.length - 1;
int left = 0, right = matrix[0].length - 1;
List<Integer> res = new ArrayList<>();
while (top <= bottom && left <= right) {
for (int i = left; i <= right; i++)
res.add(matrix[top][i]);
top++;
for (int i = top; i <= bottom; i++)
res.add(matrix[i][right]);
right--;
if (top <= bottom) {
for (int i = right; i >= left; i--)
res.add(matrix[bottom][i]);
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--)
res.add(matrix[i][left]);
left++;
}
}
return res;
}
}
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function (matrix) {
let top = 0, bottom = matrix.length - 1;
let left = 0, right = matrix[0].length - 1;
const res = [];
while (top <= bottom && left <= right) {
for (let i = left; i <= right; i++) res.push(matrix[top][i]);
top++;
for (let i = top; i <= bottom; i++) res.push(matrix[i][right]);
right--;
if (top <= bottom) {
for (let i = right; i >= left; i--) res.push(matrix[bottom][i]);
bottom--;
}
if (left <= right) {
for (let i = bottom; i >= top; i--) res.push(matrix[i][left]);
left++;
}
}
return res;
};
from typing import List
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
top, bottom = 0, len(matrix) - 1
left, right = 0, len(matrix[0]) - 1
res = []
while top <= bottom and left <= right:
for i in range(left, right + 1):
res.append(matrix[top][i])
top += 1
for i in range(top, bottom + 1):
res.append(matrix[i][right])
right -= 1
if top <= bottom:
for i in range(right, left - 1, -1):
res.append(matrix[bottom][i])
bottom -= 1
if left <= right:
for i in range(bottom, top - 1, -1):
res.append(matrix[i][left])
left += 1
return res
impl Solution {
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
// signed bounds: right/bottom can drop to -1
let (mut top, mut bottom) = (0i32, matrix.len() as i32 - 1);
let (mut left, mut right) = (0i32, matrix[0].len() as i32 - 1);
let mut res = Vec::with_capacity(matrix.len() * matrix[0].len());
while top <= bottom && left <= right {
for i in left..=right {
res.push(matrix[top as usize][i as usize]);
}
top += 1;
for i in top..=bottom {
res.push(matrix[i as usize][right as usize]);
}
right -= 1;
if top <= bottom {
for i in (left..=right).rev() {
res.push(matrix[bottom as usize][i as usize]);
}
bottom -= 1;
}
if left <= right {
for i in (top..=bottom).rev() {
res.push(matrix[i as usize][left as usize]);
}
left += 1;
}
}
res
}
}