vector<int> spiralOrder(vector<vector<int>>& matrix) {
int rows = matrix.size(), cols = matrix[0].size();
vector<vector<bool>> seen(rows, vector<bool>(cols, false));
int dr[4] = {0, 1, 0, -1}, dc[4] = {1, 0, -1, 0};
vector<int> result;
int r = 0, c = 0, d = 0;
for (int k = 0; k < rows * cols; k++) {
result.push_back(matrix[r][c]);
seen[r][c] = true;
int nr = r + dr[d], nc = c + dc[d];
if (nr < 0 || nr >= rows || nc < 0 || nc >= cols || seen[nr][nc]) d = (d + 1) % 4;
r += dr[d];
c += dc[d];
}
return result;
}
public List<Integer> spiralOrder(int[][] matrix) {
int rows = matrix.length, cols = matrix[0].length;
boolean[][] seen = new boolean[rows][cols];
int[] dr = {0, 1, 0, -1}, dc = {1, 0, -1, 0};
List<Integer> result = new ArrayList<>();
int r = 0, c = 0, d = 0;
for (int k = 0; k < rows * cols; k++) {
result.add(matrix[r][c]);
seen[r][c] = true;
int nr = r + dr[d], nc = c + dc[d];
if (nr < 0 || nr >= rows || nc < 0 || nc >= cols || seen[nr][nc]) d = (d + 1) % 4;
r += dr[d];
c += dc[d];
}
return result;
}
def spiral_order(matrix):
rows, cols = len(matrix), len(matrix[0])
seen = [[False] * cols for _ in range(rows)]
dr, dc = [0, 1, 0, -1], [1, 0, -1, 0]
result = []
r, c, d = 0, 0, 0
for k in range(rows * cols):
result.append(matrix[r][c])
seen[r][c] = True
nr, nc = r + dr[d], c + dc[d]
if nr < 0 or nr >= rows or nc < 0 or nc >= cols or seen[nr][nc]: d = (d + 1) % 4
r += dr[d]
c += dc[d]
return result
function spiralOrder(matrix: number[][]): number[] {
const rows = matrix.length, cols = matrix[0].length;
const seen = Array.from({ length: rows }, () => new Array<boolean>(cols).fill(false));
const dr = [0, 1, 0, -1], dc = [1, 0, -1, 0];
const result: number[] = [];
let r = 0, c = 0, d = 0;
for (let k = 0; k < rows * cols; k++) {
result.push(matrix[r][c]);
seen[r][c] = true;
const nr = r + dr[d], nc = c + dc[d];
if (nr < 0 || nr >= rows || nc < 0 || nc >= cols || seen[nr][nc]) d = (d + 1) % 4;
r += dr[d];
c += dc[d];
}
return result;
}
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> result;
int top = 0, bottom = matrix.size() - 1, left = 0, right = matrix[0].size() - 1;
while (top <= bottom && left <= right) {
for (int c = left; c <= right; c++) result.push_back(matrix[top][c]);
top++;
for (int r = top; r <= bottom; r++) result.push_back(matrix[r][right]);
right--;
if (top <= bottom) {
for (int c = right; c >= left; c--) result.push_back(matrix[bottom][c]);
bottom--;
}
if (left <= right) {
for (int r = bottom; r >= top; r--) result.push_back(matrix[r][left]);
left++;
}
}
return result;
}
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<>();
int top = 0, bottom = matrix.length - 1, left = 0, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
for (int c = left; c <= right; c++) result.add(matrix[top][c]);
top++;
for (int r = top; r <= bottom; r++) result.add(matrix[r][right]);
right--;
if (top <= bottom) {
for (int c = right; c >= left; c--) result.add(matrix[bottom][c]);
bottom--;
}
if (left <= right) {
for (int r = bottom; r >= top; r--) result.add(matrix[r][left]);
left++;
}
}
return result;
}
def spiral_order(matrix):
result = []
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): result.append(matrix[top][c])
top += 1
for r in range(top, bottom + 1): result.append(matrix[r][right])
right -= 1
if top <= bottom:
for c in range(right, left - 1, -1): result.append(matrix[bottom][c])
bottom -= 1
if left <= right:
for r in range(bottom, top - 1, -1): result.append(matrix[r][left])
left += 1
return result
function spiralOrder(matrix: number[][]): number[] {
const result: 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++) result.push(matrix[top][c]);
top++;
for (let r = top; r <= bottom; r++) result.push(matrix[r][right]);
right--;
if (top <= bottom) {
for (let c = right; c >= left; c--) result.push(matrix[bottom][c]);
bottom--;
}
if (left <= right) {
for (let r = bottom; r >= top; r--) result.push(matrix[r][left]);
left++;
}
}
return result;
}
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int rows = matrix.size(), cols = matrix[0].size();
const int DONE = -1000;
int dr[4] = {0, 1, 0, -1}, dc[4] = {1, 0, -1, 0};
vector<int> result;
int r = 0, c = 0, d = 0;
for (int k = 0; k < rows * cols; k++) {
result.push_back(matrix[r][c]);
matrix[r][c] = DONE;
int nr = r + dr[d], nc = c + dc[d];
if (nr < 0 || nr >= rows || nc < 0 || nc >= cols || matrix[nr][nc] == DONE) d = (d + 1) % 4;
r += dr[d];
c += dc[d];
}
return result;
}
public List<Integer> spiralOrder(int[][] matrix) {
int rows = matrix.length, cols = matrix[0].length;
final int DONE = -1000;
int[] dr = {0, 1, 0, -1}, dc = {1, 0, -1, 0};
List<Integer> result = new ArrayList<>();
int r = 0, c = 0, d = 0;
for (int k = 0; k < rows * cols; k++) {
result.add(matrix[r][c]);
matrix[r][c] = DONE;
int nr = r + dr[d], nc = c + dc[d];
if (nr < 0 || nr >= rows || nc < 0 || nc >= cols || matrix[nr][nc] == DONE) d = (d + 1) % 4;
r += dr[d];
c += dc[d];
}
return result;
}
def spiral_order(matrix):
rows, cols = len(matrix), len(matrix[0])
DONE = -1000
dr, dc = [0, 1, 0, -1], [1, 0, -1, 0]
result = []
r, c, d = 0, 0, 0
for k in range(rows * cols):
result.append(matrix[r][c])
matrix[r][c] = DONE
nr, nc = r + dr[d], c + dc[d]
if nr < 0 or nr >= rows or nc < 0 or nc >= cols or matrix[nr][nc] == DONE: d = (d + 1) % 4
r += dr[d]
c += dc[d]
return result
function spiralOrder(matrix: number[][]): number[] {
const rows = matrix.length, cols = matrix[0].length;
const DONE = -1000;
const dr = [0, 1, 0, -1], dc = [1, 0, -1, 0];
const result: number[] = [];
let r = 0, c = 0, d = 0;
for (let k = 0; k < rows * cols; k++) {
result.push(matrix[r][c]);
matrix[r][c] = DONE;
const nr = r + dr[d], nc = c + dc[d];
if (nr < 0 || nr >= rows || nc < 0 || nc >= cols || matrix[nr][nc] === DONE) d = (d + 1) % 4;
r += dr[d];
c += dc[d];
}
return result;
}
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> result;
int rows = matrix.size(), cols = matrix[0].size();
int r = 0, c = -1, dir = 1;
while (rows > 0 && cols > 0) {
for (int i = 0; i < cols; i++) {
c += dir;
result.push_back(matrix[r][c]);
}
rows--;
for (int i = 0; i < rows; i++) {
r += dir;
result.push_back(matrix[r][c]);
}
cols--;
dir = -dir;
}
return result;
}
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<>();
int rows = matrix.length, cols = matrix[0].length;
int r = 0, c = -1, dir = 1;
while (rows > 0 && cols > 0) {
for (int i = 0; i < cols; i++) {
c += dir;
result.add(matrix[r][c]);
}
rows--;
for (int i = 0; i < rows; i++) {
r += dir;
result.add(matrix[r][c]);
}
cols--;
dir = -dir;
}
return result;
}
def spiral_order(matrix):
result = []
rows, cols = len(matrix), len(matrix[0])
r, c, direction = 0, -1, 1
while rows > 0 and cols > 0:
for i in range(cols):
c += direction
result.append(matrix[r][c])
rows -= 1
for i in range(rows):
r += direction
result.append(matrix[r][c])
cols -= 1
direction = -direction
return result
function spiralOrder(matrix: number[][]): number[] {
const result: number[] = [];
let rows = matrix.length, cols = matrix[0].length;
let r = 0, c = -1, dir = 1;
while (rows > 0 && cols > 0) {
for (let i = 0; i < cols; i++) {
c += dir;
result.push(matrix[r][c]);
}
rows--;
for (let i = 0; i < rows; i++) {
r += dir;
result.push(matrix[r][c]);
}
cols--;
dir = -dir;
}
return result;
}